我正在製作一個Django應用程序,用於跟蹤電視節目劇集。這是針對某個Show實例的頁面。當用戶單擊以添加/減去季節時,我希望頁面將它們重定向到相同的詳細信息視圖,現在我已將它顯示在顯示所有Show實例列表的索引中。Django:使用基於類的視圖POST方法後重定向到同一頁
顯示,detail.html
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'),
我得到的時候我的錯誤嘗試
return redirect('show:detail')
這是錯誤
NoReverseMatch at /Daredevil/addseason/
Reverse for 'detail' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
嘿,我想,它沒有工作。我包含了我的展示細節視圖。如果可以,請幫助我。 – user298519
你得到的錯誤是什麼,它是一樣的嗎?你可以發佈你的urlpatterns? –
同樣的錯誤,並編輯我的帖子來顯示我的urlpatterns! – user298519