Adding Template

Adding Template

Adding a Template to the Application

In the previous chapter, we managed to display the content of a database table in a web page:

To add some HTML around the values, we will create a template for the application.

All templates must be located in the templates folder off your app, if you have not already created a templates folder, do it now.

In the templates folder, create a file named index.html, with the following content:

members/templates/index.html:

Did you notice the {%  %} and {{  }} parts? They are called template tags.

Template tags allows you to perform logic and render variables in your templates, you will learn more about template tags later.


Modify the View

Change the index view to include the template:

members/views.py:

The index view does the following:

  • Creates a mymembers object with all the values of the Members model.
  • Loads a the index.html template.
  • Creates an object containing the mymember object.
  • Sends the object to the template.
  • Outputs the HTML that is rendered by the template.

In the browser window, type 127.0.0.1:8000/members/ in the address bar.

The result:

Translate »