I believe I have configure everything properly, but I can't seem to figure out what is causing the error message.
What I'm trying to do is applying a view that allows me to edit a blog post.
Error message:
Using the URLconf defined in diyblog.urls, Django tried these URL patterns, in this order:
admin/
blog/ [name='index']
blog/ blogs/ [name='all-posts']
blog/ <int:post_id> [name='post-details']
blog/ new_post/ [name='new-post']
blog/ edit_post/<int:post_id> [name='edit-post']
summernote/
^media/(?P<path>.*)$
The current path, blog/edit_post/post, didn’t match any of these.
View:
def edit_post(request, post_id):
post = Post.objects.get(id=post_id)
edit_form = PostForm(instance=post)
if request.method == 'POST':
if edit_form.is_valid():
mod_post = edit_form.save(commit=False)
mod_post.save()
return redirect(get_all_posts)
context_dict = {
'edit_form': edit_form
}
return render(request, 'blog/edit_post.html', context=context_dict)
Form:
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'body',)
exclude = ['author', ]
widgets = {
'body': SummernoteWidget(),
}
url patterns:
urlpatterns = [
path('', views.index, name='index'),
path('blogs/', views.get_all_posts, name='all-posts'),
path('<int:post_id>', views.get_post_details, name='post-details'),
path('new_post/', views.add_new_post, name='new-post'),
path('edit_post/<int:post_id>', views.edit_post, name='edit-post')
]
Template:
<h1>Edit Post</h1>
<form action="post" method='post'>
{% csrf_token %}
{{ edit_form.as_p }}
<button name="submit">Save changes</button>
<form>
Can anyone help me figure out why I'm receiving this error message and why it's happening?
CodePudding user response:
First, the action attribute of the form tag isn't supposed to be there. What it does is specify the url to which the form request will be made, relative to the current document url. In this case it replaced the last part of the url (integer id of the edited post) to plainly '/post', thus throwing 404 as your server didn't expect requests to that path.
(The POST request was being sent to /blog/edit_post/post, whereas if you remove the action attribute, it will be sent to the same address you see in the browser nav bar when you're on that page)
Secondly, your edit_post handler completely ignored the request payload, instead it found the persisted post and saved it again. Relevant documentation
The last problem with the form being empty upon being loaded is a bit trickier to explain if you don't know what's going on already. Both requests to display the page and to save the changes are located under the same url and share the same handler (edit_post
). Accordingly, the conditional clause checks if the current request method is GET (assumed by default) or POST, and depending on that follows different branches.
CodePudding user response:
'post' isn't an integer path('edit_post/<int:post_id>', views.edit_post, name='edit-post')
if you want the URL to be a string, use slug: path('edit_post/<slug:post_id>', views.edit_post, name='edit-post')