Django: redirect to the same page after the POST method using class-based views

I am making a Django app that tracks episodes of TV shows. This page is for a specific instance of Show. When the user clicks to add / subtract the season, I want the page to redirect them to the same detailed view, right now I have an index that shows a list of all Show instances.

show-detail.html

<form action="{% url 'show:addseason' show=show %}" method="post"> {% csrf_token %} <button class="btn btn-default" type="submit">+</button> </form> <form action="{% url 'show:subtractseason' show=show %}" method="post"> {% csrf_token %} <button class="btn btn-default" type="submit">-</button> </form> 

views.py

 class ShowDetail(DetailView): model = Show slug_field = "title" slug_url_kwarg = "show" template_name = 'show/show-detail.html' class AddSeason(UpdateView): model = Show slug_field = 'title' slug_url_kwarg = 'show' fields = [] def form_valid(self, form): instance = form.save(commit=False) instance.season += 1 instance.save() return redirect('show:index') class SubtractSeason(UpdateView): model = Show slug_field = 'title' slug_url_kwarg = 'show' fields = [] def form_valid(self, form): instance = form.save(commit=False) if (instance.season >= 0): instance.season -= 1 else: instance.season = 0 instance.save() return redirect('show:index') 

urls.py

 url(r'^$', views.IndexView.as_view(), name='index'), url(r'^about/$', views.AboutView.as_view(), name='about'), # form to add show url(r'^add/$', views.ShowCreate.as_view(), name='show-add'), # edit show #url(r'^(?P<show>[\w ]+)/edit/$', views.ShowUpdate.as_view(), name='show-update'), # delete show url(r'^(?P<show>[\w ]+)/delete/$', views.ShowDelete.as_view(), name='show-delete'), # signup url(r'^register/$', views.UserFormView.as_view(), name='register'), # login url(r'^login/$', views.LoginView.as_view(), name='login'), # logout url(r'^logout/$', views.LogoutView.as_view(), name='logout'), url(r'^error/$', views.ErrorView.as_view(), name='error'), url(r'^(?P<show>[\w ]+)/$', views.ShowDetail.as_view(), name='show-detail'), url(r'^(?P<show>[\w ]+)/addseason/$', views.AddSeason.as_view(), name='addseason'), url(r'^(?P<show>[\w ]+)/subtractseason/$', views.SubtractSeason.as_view(), name='subtractseason'), url(r'^(?P<show>[\w ]+)/addepisode/$', views.AddEpisode.as_view(), name='addepisode'), url(r'^(?P<show>[\w ]+)/subtractepisode/$', views.SubtractEpisode.as_view(), name='subtractepisode'), 

I get an error when I try

 return redirect('show:detail') 

This is mistake

 NoReverseMatch at /Daredevil/addseason/ Reverse for 'detail' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 
+15
source share
5 answers

For CBV:

 from django.http import HttpResponseRedirect return HttpResponseRedirect(self.request.path_info) 

To represent the function:

 from django.http import HttpResponseRedirect return HttpResponseRedirect(request.path_info) 
+20
source

redirect to the same page (e.g. http GET) after POST, I like ...

 return HttpResponseRedirect("") # from django.http import HttpResponseRedirect 

it also avoids hardcoding the show:detail route name and is a clearer intention (for me, at least!)

+13
source

I assume that you need to provide kwarg to identify the show when you redirect, although I do not see the code for your DetailView I would suggest that kwarg is called either pk or perhaps show from the convention you used in AddSeason and SubtractSeason . Try:

 redirect('show:detail', kwargs={'show': instance.pk}) 

EDIT: the name of the detail URL is 'show-detail' , so the scope name will be 'show:show-detail' (if it is in the show namespace, like other URLs). I still think kwarg is needed for this, try:

 redirect('show:show-detail', kwargs={'show': instance.pk}) 
+3
source

I know this is a really old problem, but I just ran into the same question and redirect(request.META.get('HTTP_REFERER', redirect_if_referer_not_found)) fixed my problem. It gets the http referer from the request data, but if it is not provided by the client, it will redirect you to redirect_if_referer_not_found

+2
source

This will work for viewing based on classes and function 100%:

 from django.http import HttpResponseRedirect ... return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 

or

 from django.http import HttpResponseRedirect ... return HttpResponseRedirect(self.request.META.get('HTTP_REFERER')) 

Example -

 class TaskNotificationReadAllView(generic.View): def get(self, request, *args, **kwargs): TaskNotification.objects.filter(assigned_to=request.user).update(read=True) print(request.META.get('HTTP_REFERER')) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 
0
source

All Articles