我有兩個通用視圖(“創(chuàng)建視圖”和“詳細信息視圖”)。在我的創(chuàng)建視圖中,保存我的窗體后,我想重定向到詳細信息視圖以顯示我新創(chuàng)建的對象。但是發(fā)生了一個錯誤:Reverse for 'questions.views.DisplayQuestions' not found. 'questions.views.DisplayQuestions' is not a valid view function or pattern name.如何通過reverse_lazy調(diào)用我的詳細信息視圖?.視圖:class DisplayQuestions(ListView): model = Question context_object_name = "all_questions" template_name = "questions/home.html" def get_queryset(self): return Question.objects.order_by(self.kwargs['display_type'])@method_decorator(login_required, name='dispatch')class CreateQuestion(CreateView): model = Question template_name = 'questions/nouveau.html' form_class = QuestionForm def get_success_url(self): return reverse_lazy(DisplayQuestion) # <-- This doesn't work !!! def form_valid(self, form): self.object = form.save(commit=False) self.object.profil = self.request.user.profil self.object = form.save() return HttpResponseRedirect(self.get_success_url()).網(wǎng)址:urlpatterns = [ url(r'^nouveau$', views.CreateQuestion.as_view()), url(r'(?P<display_type>\w+)', views.DisplayQuestions.as_view()),].形式:class QuestionForm(forms.ModelForm): class Meta: model = Question fields = ('question','categorie',)
1 回答

SMILET
TA貢獻1796條經(jīng)驗 獲得超4個贊
您應該將配置中的每個URL命名為:
urlpatterns = [
url(r'^nouveau$', views.CreateQuestion.as_view(), name='create-question'),
url(r'(?P<display_type>\w+)', views.DisplayQuestions.as_view(), name='display-question'),
]
然后訪問它,
return reverse_lazy('display-question', kwargs={'display_type': 'your-display-type-value'})
添加回答
舉報
0/150
提交
取消