亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django:reverse_lazy()與通用視圖

Django:reverse_lazy()與通用視圖

ITMISS 2022-09-13 15:17:29
我有兩個通用視圖(“創建視圖”和“詳細信息視圖”)。在我的創建視圖中,保存我的窗體后,我想重定向到詳細信息視圖以顯示我新創建的對象。但是發生了一個錯誤:Reverse for 'questions.views.DisplayQuestions' not found. 'questions.views.DisplayQuestions' is not a valid view function or pattern name.如何通過reverse_lazy調用我的詳細信息視圖?.視圖: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()).網址: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條經驗 獲得超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'})


查看完整回答
反對 回復 2022-09-13
  • 1 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號