Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Django views.py: Add to favorites page

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 6 months 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'>Bookmark</button>
    </form>
</td>

Here is the urls.py file

app_name = 'foobar'

urlpatterns = [
    path('', views.f_ListView.as_view(), name='overview'),
    path('<int:pk>/', views.f_DetailView.as_view(), name='detailview'),
    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': #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)

usr_profile.png

User

( 6 months 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)

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif