Python Django Tutorial: Use Of Templates - Part Seven

Introduction

 
Today, I will share one new part of Python Django from my tutorial series. In this part, you will learn how to use an HTML template with Django.
 
I already told you the following:
Django has many built-in tags and filters for use in templates with Django. Each tag and filter gives many facilities to show results on the webpage. It almost works as angularjs doe but It can’t work on run time. It works only when It loads the content the first time. But when it loads the first time it has the same features as angularJs.
 
Now today I want to show how can you use built-in templates HTML pages in Django also some basic built-in tag uses with templates.
  1. Now create a Django project and after that create the Django app in the project.
     
  2. After that create a new folder in the Django app folder and make an HTML file in this folder.
     
    folder
     
  3. Now we create a model in models.py file as below.
     
    model
     
  4. Migrate this model. If you don’t know then follow my previous articles.
     
  5. After that, we go to the admin.py file to use this model in admin for entering the values into this table of database.
     
    value
     
  6. Now open the admin panel and insert value in Publisher models.
     
    admin
     
  7. Now we want to show all the values in our templates. So now we go to urls.py files of the Django app that define URLs.
     
    value
     
  8. Now we go to views.py for access templates and send table data to templates using view file.
     
  9. We create method in views.py file as below image:
     
    method
     
  10. Here publisher variable holds the all value of publisher table and ‘templates/first.html’ denotes location HTML file.’publisher’:publisher is the context that we use in templates for accessing data fro this context type variable.
     
  11. Now we go to first.html file and write code as below.
     
    code
     
  12. In this code for loop is used for accessing each row from publisher object. In publish.id, publish.name is attributes of the table.
     
    Note: Attribute name in templates is always the same as the models.py attributes.
     
  13. Now go to URL http://127.0.0.1:8000/publisher/. Here you will get all data from the database in print on the webpage.
     
    webpage
So this is the basic HTML page use. Django has various types of facilities to use HTML pages according to your needs and has many facilities using built-in tags.


Recommended Free Ebook
Similar Articles