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="submit" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-remove"></span> add this book to favourite
</button>
my url
url(r'^book/(?P<pk>d+)$', views.BookDetailView.as_view(), name='book-detail'),
url(r'^book/(?P<pk>d+)/f/$', views.favorite, name='favorite'),
where I need fav_list
{% if fav_list %}
{% for fav in fav_list %}
{% if book in fav.favorites.all %}Favorited
{% else %}
<form action="{% url 'favorite' book.id %}" method="post" style="display:inline;">
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-remove"></span> add this book to favourite
</button>
</form>
{% endif %}
{% endfor %}
{% else %}
<form action="{% url 'favorite' book.id %}" method="post" style="display: inline;">
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-remove"></span> add this book to favourite
</button>
</form>
{% endif %}
now the problem is that when i get redirected to book_detail.html I am not getting the fav_list which is needed for the template
How do I redirect properly or use some other way to define views?
1 Answer
1
You're not redirecting from the favorite view. Upon form submission, considered a good practice to redirect.
favorite
Try
from django.shortcuts import redirect
from django.urls import reverse
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)
# if you have an app_name in urls.py file try this
# return redirect(reverse('YOUR_APP_NAME:book-detail', args=(pk,)))
return redirect(reverse('book-detail', args=(pk,)))
I hope this will help.
Anyother way to declare url for this function? Then it may work
– Ahmed
Jun 30 at 21:46
same error: Page not found (404) Request Method: GET Request URL: 127.0.0.1:8000/book/7/f/book/book-detail its works with the render but only not getting the fav_list object
– Ahmed
Jun 30 at 22:04
@Ahmed ok, it doesn't know how to resolve the path, I edited my answer, used the
reverse method. Check it out– Yusef BH
Jun 30 at 22:24
reverse
Woah it worked, Thankyou so much.
– Ahmed
Jul 1 at 9:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
gives error: Page not found (404) Request Method: GET Request URL: 127.0.0.1:8000/book/5/f/book_detail.html
– Ahmed
Jun 30 at 21:38