Python Django Tutorial: Use Of Templates - Part Seven

Today, I will share one new part of Python Django from my tutorial series. In this part you will learn the how to use html template with django.

I already told you the following:

Django has many built-in tags and filtesr for using in templates with django. Each tag and filter gives many facilities to show results on webpage. It almost works as angularjs doe sbut 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 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 django project and after that create django app in project.

  2. After that create new folder in django app folder and make html file in this folder.

    folder

  3. Now we create model in models.py file as below.

    model

  4. Migrate this models. If you don’t know then follow my previous articles.

  5. After that we go to admin.py file to use this model in admin for entering value into this table of database.

    value

  6. Now open admin panel and insert value in Publisher models.

    admin
  7. Now we want show all values in our templates. So now we go to urls.py files of 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 context that we use in templates ofr 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 table.

    Note: Attribute name in templates is always 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 database in print in 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.

Up Next
    Ebook Download
    View all
    Learn
    View all