Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 enter image description here. 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'),

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading