简体   繁体   English

在 CreateView 中重定向 Django 不起作用

[英]Redirect in CreateView Django doesn't work

Hi I try to make a blog using CBV.您好,我尝试使用 CBV 创建博客。 I want after create a new post in post_form.html the CreateView will redirect to the new post_detail I just made.我想在 post_form.html 中创建一个新帖子后,CreateView 将重定向到我刚刚创建的新 post_detail。 So I was search on gg and try both get_success_url and redirect_field_name.所以我在 gg 上搜索并尝试了 get_success_url 和 redirect_field_name。 But it still had error Page not found但它仍然有找不到页面的错误在此处输入图像描述 . . Because of that I really don't know the new post was created or not.因此,我真的不知道新帖子是否已创建。 Can someone check it for me.有人可以帮我检查一下吗?

Views.py视图.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,})

urls.py网址.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'),

The problem is not the success URL, it is the method to which the form posts.问题在于成功 URL,而是表单发布到的方法。 You should make a POST request to the post_new view.您应该向post_new视图发出 POST 请求。 Your <form> should thus specify:因此,您的<form>应该指定:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM