How to add data from Queryset into Templates – Django? (Part XII)

In this tutorial we are going to learn how to take dynamic data already saved in the database and use it to show up on the main website with the help of QuerySet.

This tutorial is a part of our series on Creating Blog Website using Django.

Link to the Previous tutorial:

How to filter and order objects along with publishing them using QuerySet ?(Part XI)

Why add Dynamic data in Django?

Till now, we have inputed data (titles, header, paragraph text) directly into the HTML file which is shown up on the website. But content is changing and getting updated everyday; so it would be a very tedious task to change the HTML file every single time you want to make a change.

Here’s, when use of QuerySet to retrieve data from the database directly into the HTML file. Then, we just need to take care to make changes in the database; and the website will get updated automatically.

Importance of Views in Django

Remember, earlier we have created blog1/views.py file. In that, we have written the code in order to render the HTML file, something like this :

from django.shortcuts import render

# Create your views here.
def post(request):
    return render(request, 'blog1/post.html', {})

Now, that’s what views do; they are primarily used to connect the models and templates in order to display the exact content we want it to and in a way we want it to- taking care of both the objects created using model file and using the template file to display it appropriately as determined in the HTML file.




 

Adding access to database in the Views

Now, go to your Blog directory and open up the views.py file.

But, to have access to the data, we need to include the models.py file; in order to determine which objects are created using this particular model (framework).

For that, add the command given below on the top of the views.py file.

from .models import Post

(Note that, as previously mentioned, ‘.’ operator is used to tell the interpreter that the file in saved in the same directory and it helps to reduce the hassle of writing in the full directory address.)

So, we want only the published posts to appear on the website in the order they published. For that we are going to use the filter and order query alongside.

If you want to know more about QuerySet, have a look at :

What are Django QuerySet and to execute one ? (Part X)

To filter and sort the posts, use the command :

Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')

Now, for easy access we will create an instance of the above queryset :

orderedpost = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')

 

Add this also to the views.py file. Now, the file will look something like this :

from django.shortcuts import render
from django.utils import timezone
from .models import Post

# Create your views here.
def post(request):
    orderedpost = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog1/post.html', {})

But, we have not added the instance to the template file, which is actually getting rendered.

Don’t worry, that’s something we are going to do in the next and final post.

Next part of this Django tutorial series:

 

That’s it for now.

Feel free to drop any doubts in the comments section below.

Also have a look at,

Web Page Template Using Languages HTML, CSS and Bootstrap

How To Create a Navigation Bar in Bootstrap


Leave a Reply

Your email address will not be published. Required fields are marked *