Django views.py: Add to favorites page

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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

Here is the .html file

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

Here is the urls.py file

app_name = 'foobar'

urlpatterns = [
    path('', views.f_ListView.as_view(), name='overview'),
    path('/', views.f_DetailView.as_view(), name='detailview'),
    path('/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': #Then add this video to users' favourite
        video.

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

How to edit the views.py file so it is registered in the database that the user has favorited this video ?

Here is the models.py file

from django.contrib.auth.models import AbstractUser

class ProjectUser(AbstractUser):

    def __str__(self):
        return self.email

class Video(models.Model):
    name = models.CharField(max_length=255),
    videofile = models.FileField(upload_to="static/videos/"),
    favourite = models.ManyToManyField(ProjectUser, related_name="fav_videos", blank=True)
profilepic.png
manpreet 2 years ago
def favourite_post(request, fav_id):
video = get_object_or_404(Video, id=fav_id)
    if request.method == 'POST':
        video.favourite.add(request.user)
        # Rest code
    return render(request, 'foobar/%s' % fav_id)

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community