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'),
CodePudding user response:
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>