Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

python 3.x - How to bookmark a page in Django?

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

Here is the .html file

<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>

Here is the urls.py file

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

Here is the view.py file

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • First you modify the models.py that has the user models
class ProjectUser(AbstractUser):
    images = models.ManyToManyField(Images)

    def __str__(self):
        return self.email
  • 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 %}
  • 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()
  • Create a forms.py file in your add and add the following:
class ImageForm(forms.Form):
    image = forms.ModelChoiceField(queryset=Images.objects.all())

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


  • In the urls.py add the following:

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

  • In models.py add a method in User model for getting bool if video is bookmarked
def is_bookmarked(self, video_id): 
    return self.bookmarked_videos.filter(id=video_id).exists()

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

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

`{% if video.is_bookmarked %}`

  • Delete the UserProfile as you do not need it. Just make sure to have needed instance in context of view.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...