如何在 Django 中为页面添加书签? [英] How to bookmark a page in Django?

查看:29
本文介绍了如何在 Django 中为页面添加书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑一个 html 页面,以便登录用户可以收藏/收藏 video.id

I am trying to edit an html page so a logged in user can favorite / bookmark a video.id

这是.html文件

<td>
    <form method='POST' action="{% url 'researcher_view_app:favourite_post' video.id %}">
        {% csrf_token %}
        <input type='hidden' name='video' value={{ video.id }}>
        <button type='submit'>Bookmark</button>
    </form>
</td>

这是urls.py文件

path('<int:fav_id>/favourite_post', views.favourite_post, name='favourite_post'),

这是view.py文件

def favourite_post(request, fav_id):
    video = get_object_or_404(Video, id=fav_id)
    if request.method == 'POST':
        video.

   return render(request, 'researcher_view_app/%s' % fav_id)

推荐答案

  • 首先修改具有用户模型的 models.py
  • class ProjectUser(AbstractUser):
        images = models.ManyToManyField(Images)
    
        def __str__(self):
            return self.email
    

    • .html 文件中添加以下内容:
      • In the .html file add the following:
      • {% for image in available_images %}
        /* show image */
        <form method='post' action="{% url 'user-image-add' %}">
         {% csrf_token %}
         <input type='hidden' name='image' value={{image.id}}>
         <button type='submit'>bookmark</button>
        </form>
        {% endfor %}
        

        • 在您的 views.py 中添加以下方法
          • In your views.py add the following method
          • def user_image_add(request):
                user_image_form = ImageForm(request.POST or None)
                if request.method == 'POST' and user_image_form.is_valid(): 
                     request.user.images.add(user_image_form.cleaned_data['image'])
                     return JsonResponse(status=200)
                raise Http404()
            

            • 在您的添加中创建一个 forms.py 文件并添加以下内容:
              • Create a forms.py file in your add and add the following:
              • class ImageForm(forms.Form):
                    image = forms.ModelChoiceField(queryset=Images.objects.all())
                

                要显示那些带有书签的图像,您可以像上面的代码一样遍历 request.user.images(它为您提供图像的 QS).

                To show those bookmarked images you can just iterate over request.user.images (it gives you a QS of Images) similar to code above.

                • urls.py 中添加以下内容:
                • In the urls.py add the following:

                path('user-image-add/', views.user_image_add, 'user-image-add')

                • 在models.py中,在用户模型中添加一个方法,用于在视频被添加书签时获取布尔值
                def is_bookmarked(self, video_id): 
                    return self.bookmarked_videos.filter(id=video_id).exists()
                

                同样可以将 is_bookmarked 添加到接受 user_id 并检查 video.projectuser_set 的 Video 模型.

                simirlarly is_bookmarked can be added to Video model accepting user_id and checking video.projectuser_set.

                并将以下内容添加到用户为视频添加书签的 .html 文件中

                And add the following to your .html file where users bookmarked a video

                `{% if video.is_bookmarked %}`
                

                <小时>

                • 删除不需要的 UserProfile.只需确保在视图上下文中具有所需的实例.

                  • Delete the UserProfile as you do not need it. Just make sure to have needed instance in context of view.
                  • 这篇关于如何在 Django 中为页面添加书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆