I'm making a Django app that keeps track of tv show episodes. This is for a page on a certain Show instance. When a user clicks to add/subtract a season, I want the page to redirect them to the same detail view, right now I have it on the index that shows the 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 the error
NoReverseMatch at /Daredevil/addseason/
Reverse for 'detail' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
For CBV:
from django.http import HttpResponseRedirect
return HttpResponseRedirect(self.request.path_info)
For function view:
from django.http import HttpResponseRedirect
return HttpResponseRedirect(request.path_info)
You can achieve this by redirecting to HTTP_REFERER
header and for fallback just add another path.
Example snippet:
return redirect(request.META.get('HTTP_REFERER', 'redirect_if_referer_not_found'))
to redirect to the same page (e.g. an http GET) after a POST, I like...
return HttpResponseRedirect("") # from django.http import HttpResponseRedirect
it also avoids hardcoding the show:detail
route name, and is a lil' clearer intention wise (for me at least!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With