The edit_entry URL
- The URL for the page needs to pass the ID of the entry to be edited.
urls.py
python
23#--snip--
24urlpatterns = [
25 #--snip--
26 #Page for editing an existing entry
27 path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry'),
28]- The ID passed in the URL (ex: http://localhost:8000/edit_entry/1/) is stored in the parameter entry_id.
- The URL pattern sends requests that match this format to the view function edit_entry().
The edit_entry View Function:
- When the edit_entry page receives a GET request, edit_entry() will return a form for editing the entry.
- When the page receives a POST request, with the revised entry text, it will save the modified text into our database:
views.py

โsnipโ

- In line 5, we import the Entry model.
- In line 75, we get the entry object that the user wants to edit and the topic associated with the entry.
- In the IF block, which runs for a GET request, we make an instance of EntryForm with the argument instance=entry. This argument tells Django to create the form prefilled with information from the existing entry object.
- When processing a POST request, we pass the instance=entry argument and the data=request.POST argument.
- This tells Django to create a form instance based on the info associated with the existing entry object, updated with any relevant data from request.POST
- We then redirect to the topic page.
The edit_entry Template:
- Create the edit_entry template. The template for edit_entry is similar to the template for the new_entry:
edit_entry.html

- In line 8, the action argument sends the form back to the edit_entry() function for processing
- Note that in line 8, we include the entry ID as an argument in the {% url %} tag, so the view function can modify the correct entry object.
Linking to the edit_entry Page:
- Include a link to the edit_entry page for each entry on the topic page:
topic.html

- Include the edit link after each entryโs date and text has been displayed
- Use the {% url %} template tag to determine the URL for the named URL pattern edit_entry, along with the ID attribute of the current entry in the loop, entry.id.