DEV Community

balt1794
balt1794

Posted on • Updated on

Chapter 4: Listings Page - Django 3...2...1...Takeoff Series

Let’s continue building our website by creating a template for our listings page and displaying the listings that we have created.

To do this, we follow the same steps as in the previous chapter. We create a URL, a view, and a template.

Open urls.py and add the path to the listings page. I called it all_listings here, but you can choose your own names or stick to the ones in the book if you prefer.

Listings URL Path

Screen Shot 2020-09-15 at 8.32.35 PM

Most of the concepts were already explained in the last chapter. Here, we just added the path to the listings page.

Screen Shot 2020-09-15 at 8.32.48 PM

Listings View

Open views.py and add the following code.

Full Code

Screen Shot 2020-09-15 at 8.33.58 PM

Shortened Version

Screen Shot 2020-09-15 at 8.34.09 PM

from .models import model_name

The reason why we import the Listings model into views.py is that we need the data from the model, so that we can pass it to the all_listings template. Once we pass the data to the template, we can use it and display it to the users. The index view didn’t require any data to be passed, that’s why we didn’t import any model before.

Database Queries

In order to create, retrieve, update, and delete objects from the database, we need to query the database. Django provides us with a database-abstraction API in order to perform the actions mentioned before. Assuming you have already set your database models, you can use queries to manage the data stored in the database.

Let’s describe the flow of the code below. By doing so, I will explain the query being used here.

Screen Shot 2020-09-15 at 8.35.22 PM

Before the equal sign (=), we have all_listings which refers to the variable which will hold the return data that we are prompting the database to give us. You can set your variables to have any name.

After the equal sign (=), we have the model name Listings, the objects attribute of the model, and the method order_by() which gives back the listings from newest to oldest based on the date.

The entire query prompts the database for all the objects in the Listings model arranged by date in reverse order (-). All objects will be stored in the variable all_listings which will be stored in the context = {} dictionary and passed to the all_listings template by using the render method.

I’ll explain a few more queries here, but if you like you can check out the official Django documentation for more extensive explanations and more queries.

variable_name = model_name.objects.all()

Returns all objects from database for the specified model_name

variable_name = model_name.objects.first()

Returns first object from database for the specified model_name

variable_name = model_name.objects.last()

Returns last object from database for the specified model_name

variable_name = model_name.objects.get(name= ‘Sam’)

Returns object/objects from database that match name= ‘Sam’ for the specified model_name

variable_name = model_name.objects.order_by(‘list_date’)

Returns object/objects from database ordered by list_date for the specified model_name

Listings Template

Templates are usually lengthy for this project, so if you feel overwhelmed by them, please refer to the source code. We can have an entire book about the HTML code being used here. I won’t focus on it too much for this book since this is a Python/Django book, but feel free to check online for HTML code that you don’t understand.

Screen Shot 2020-09-16 at 9.05.30 PM

Django provides us with built-in template tags that we can use in our templates to run Python code.

% for listing in all_listings % – % empty % – % endfor %

Since we have passed the all_listings dictionary to this template, we can use the data stored in the dictionary by calling the variable which holds the data in this case all_listings. We loop through the data in all_listings and display specific fields by using dot notation (.) to access the dictionary data, for instance, listing.title for the title of the listing.

If there are not listings, the empty clause will display the text, “No listings found. Try again”.

The endfor clause ends the loop.

We have also added a link to view each listing. As of right now, the link doesn’t take you anywhere, but later on we will activate it. Small details like that which are not relevant yet will be added here and there as we move along.

Unfortunately, I can’t explain every single line of code, so please consult and investigate the rest of the code. I believe this s a good way to get the ball rolling when it comes to searching and understanding code on your own.

Let’s run the server by issuing python manage.py runserver and go to http://127.0.0.1:8000/all_listings/

Picture1

You should see a page with all listings you’ve created so far. If you don’t see the listings, go back to through the chapter and troubleshoot.

If you're enjoying the series and want to support, you can find the entire book below.

Django 3…2…1…Takeoff! Kindle

Django 3…2…1…Takeoff! Paperback

Top comments (0)