簡體   English   中英

在 CreateView 中重定向 Django 不起作用

[英]Redirect in CreateView Django doesn't work

您好,我嘗試使用 CBV 創建博客。 我想在 post_form.html 中創建一個新帖子后,CreateView 將重定向到我剛剛創建的新 post_detail。 所以我在 gg 上搜索並嘗試了 get_success_url 和 redirect_field_name。 但它仍然有找不到頁面的錯誤在此處輸入圖像描述 . 因此,我真的不知道新帖子是否已創建。 有人可以幫我檢查一下嗎?

視圖.py

class PostCreateView(LoginRequiredMixin,CreateView):
    login_url = '/login/'
    form_class = forms.Post_form
    model = models.Post
    #redirect_field_name = 'mlog/post_detail.html'

    def get_success_url(self):
        return reverse('post_detail', kwargs={'pk': self.object.pk,})

網址.py

path('post/<int:pk>/',views.PostDetailView.as_view(),name='post_detail'),
path('post/new/',views.PostCreateView.as_view(),name='post_new'),
path('post/<int:pk>/edit/',views.PostUpdateView.as_view(),name='post_edit'),
path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'),
path('dratf/',views.DraftListView.as_view(),name='post_draft_list'),

問題在於成功 URL,而是表單發布到的方法。 您應該向post_new視圖發出 POST 請求。 因此,您的<form>應該指定:

<form method="POST" action="{% url 'post_new' %}">
    …
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM