Chapter 9: Edit Listing Page - Django 3...2...1...Takeoff! Series
Tags: django, python, webdev, tutorial
Published: 2020-09-21 00:59:00

You can create new listings, see the details of the listings, but you can’t edit or delete a listing. Let’s take care of the editing part in this chapter.

**Edit Listing URL**

Open urls.py and add the following code.

![Screen Shot 2020-09-20 at 8.50.05 PM](https://dev-to-uploads.s3.amazonaws.com/i/9ip1h67ix47upk0mxswp.png)
 
In a similar way to the detail view, we need to include the ID of the listing in the URL, so that we can edit the correct listing. 

**Edit Listing View**

Since we are dealing with changing a form, the edit listing view will be different from the detail view. Open views.py and add the following code.

![Screen Shot 2020-09-20 at 8.50.36 PM](https://dev-to-uploads.s3.amazonaws.com/i/dvcy1x66hj0lx97yh1n4.png)
 
The main difference between the new listing view and the edit view is that we are not creating but rather editing an existing listing. 

**listing = Listings.objects.get(id=detail_id)**

First, we get the specific listing we are trying to edit. After we do this, then the process is very similar to the new listing view. Refer to chapter 5 for an explanation of how forms work.

**form = ListingForm(instance=listing)**

If the request is not a POST request, we generate a form, but this time the fields will be already filled with the existing information for that particular listing. This is achieved by setting instance=listing which gets a particular instance from the database.

**form = ListingForm(request.POST, request.FILES, instance=listing)**

We want to edit an existence model instance in the database, we use instance=listing, so that we overwrite that particular instance in the database when submitting the form again.

**Edit Listing Template**

Create a template called edit_listing.html in the templates folder. Open this template and add the following code.

![Screen Shot 2020-09-20 at 8.51.52 PM](https://dev-to-uploads.s3.amazonaws.com/i/coqmeq7n252zm22qes0q.png)

**form action="% url 'listings:edit_listing' listing.id %" method='post' enctype="multipart/form-data"**

The action method changed since we are in a different template now and we also included listing.id which is needed to edit the correct listing when submitting the form.

**My Listings Template Revisited**

Open my_listings.html and activate the edit button.

![Screen Shot 2020-09-20 at 8.52.50 PM](https://dev-to-uploads.s3.amazonaws.com/i/8qzrau7kb344ljsqcr6g.png)
 
**a class="btn" href="% url 'listings:edit_listing' my_listing.id %"">Edit/a**

We activated the edit button by adding the path to the edit page and passed the ID of the particular listing we want to edit.

Run the server, go to my listings page, and edit some listings to test the form. After you submit the form, the listings should reflect the changes being made.

![j](https://dev-to-uploads.s3.amazonaws.com/i/xfvng0iguat9cvxvzf2q.png)

![Screen Shot 2020-09-20 at 8.54.00 PM](https://dev-to-uploads.s3.amazonaws.com/i/v5xkthb88rm0g0hc0u57.png)
  
If you're enjoying the series and want to support, you can find the entire book below.

[*Django 3…2…1…Takeoff! Kindle*
](https://www.amazon.com/Django-Takeoff-Quick-Learning-Development/dp/B08HB1CWHW/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=&sr=)

[*Django 3…2…1…Takeoff! Paperback*
](https://www.amazon.com/Django-Takeoff-Quick-Learning-Development/dp/B08HB1CWHW/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=&sr=)