1👍
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.
0👍
‘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')
- [Answered ]-Django ManyToManyField reverse Relationship
- [Answered ]-Symmetric django model
- [Answered ]-Use Django ORM in locust
- [Answered ]-Django deferring save() of graph of model objects / transactionally create models
- [Answered ]-Practical Django Projects – Pages 71 and 80