Posts

Showing posts with the label django-models

Redirect with get_context_data in generic detailview django

Redirect with get_context_data in generic detailview django I have this view where i needed two models Book and UserFav so I used context for that: class BookDetailView(LoginRequiredMixin, generic.DetailView): model = Book template_name = 'book_detail.html' def get_context_data(self, **kwargs): context = super(BookDetailView, self).get_context_data(**kwargs) context.update({ 'fav_list': UserFav.objects.filter(user=self.request.user) }) return context and another view function def favorite(request, pk): book = get_object_or_404(Book, pk=pk) fav, created = UserFav.objects.get_or_create(user=request.user) fav.favorites.add(book) return render(request, 'book_detail.html', {'book': book}) form for the favourite function <form action="{% url 'favorite' book.id %}" method="post" style="display: inline;"> {% csrf_token %} <button type="subm...