Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

django - Redirect to same page after POST method using class based views

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: []
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...