Django Tutorial
About Lesson

In this article i want to show you Rendering Database Data in Django Templates, so as you know we are using SQLite database and we have inserted some data in to our database, now we want to render that data in to our html template, also we will learn that how you can use for loop for iterating over the data.

 

 

Now open your views.py file, if you remember we have a Polls view function, so we want to retrieve our data in this view function and after that we want to render this data in to our polls_list.html.

 

In the above view function first we have retrieved our data from Question model, we are using all() method because i want to get all the data and after that i want to send the data through context variable to our html file.

 

 

So now this is our complete views.py file.

 

 

 

Now open your polls_list.html file, because we want to retrieve the data in this file, so add this code. in here we are using for loop to iterate over the data in our database and render that data in to a list.

 

 

 

This is our complete code for polls_list.html.

 

 

 

 

Now run the code and this is the result.

Rendering Database Data in Django Templates
Rendering Database Data in Django Templates

 

 

 

Rest of the file for the tutorial.

 

mysite/templates/base.html

 

 

 

mysite/templates/navbar.html

 

 

 

mysite/templates/index.html 

 

 

 

mysite/polls/models.py

 

 

 

mysite/polls/admin.py

 

 

 

mysite/polls/urls.py

 

 

 

mysite/urls.py

 

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found