简体   繁体   中英

Redirect in CreateView Django doesn't work

Hi I try to make a blog using CBV. I want after create a new post in post_form.html the CreateView will redirect to the new post_detail I just made. So I was search on gg and try both get_success_url and 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

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

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. You should make a POST request to the post_new view. Your <form> should thus specify:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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