46

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: []
Braiam
  • 1
  • 11
  • 50
  • 74
user298519
  • 1,002
  • 1
  • 10
  • 25

5 Answers5

58

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)
Dmitriy Sintsov
  • 3,461
  • 29
  • 18
  • 1
    Please, what do you mean by " path_info "? Is it the path name? – ThunderBird Jun 06 '18 at 11:01
  • 2
    `path_info` is a variable stored in request. It has the current url that you're on. It's just like reloading the page. – Vineeth Sai Jan 24 '19 at 08:42
  • 1
    wouldn't `path_info` store the CBV url (i.e /subtractseason in OP's example) whereas OP wants to go back to the URL that the user is 'on' (not the URL that was hyperlinked to the item that was clicked) – Anupam Aug 06 '20 at 15:00
20

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'))
FKLC
  • 1,412
  • 3
  • 13
  • 19
17

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!)

David Lam
  • 4,339
  • 2
  • 22
  • 33
17
from django.http import HttpResponseRedirect

def someview(request):

   ...
   return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

taken from: https://stackoverflow.com/a/12758859/14020019

Kaushik Kakdey
  • 474
  • 3
  • 6
3

I am guessing you need to provide a kwarg to identify the show when you redirect, although I can't see the code for your DetailView I would assume the kwarg is called either pk or possibly show going from the convention you've 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 scoped viewname would be 'show:show-detail' (if it is in the show namespace like the other urls). I still think it would need a kwarg though, try:

redirect('show:show-detail', kwargs={'show': instance.pk})
James Hiew
  • 4,923
  • 4
  • 25
  • 35