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

查看:122
本文介绍了如何在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

    • First you modify the models.py that has the user models
    • 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 (它给出了类似于上面的代码。

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


                  • URL中.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()
                  

                  类似地已被标记为可以添加到接受user_id并检查 video.projectuser_set 的视频模型中。

                  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天全站免登陆