Blog

Templating in Django is one of the most powerful feature.Templates allow you to specify the structure of the output document. Templates are often used to create HTML documents

you can also create other types of documents also if needed like CSS, JS, XML, JSON, etc.., Django has its own template system.

The code shown below is the HTML template file. It is called by the function render().

You have to write the python code in between {% %} pounds, so that Django recognises that it is a Python code. And to display the variable in HTML template, you have to use {{ }}. Write a variable that you have to display in between 2 curly braces. You have to close the for loop and if all_posts by writing end(here for or if). See the example…

It will display all the post(s) that are present in post list in HTML template. Create the “index.html” file in and add the following code.

# filename: index.html

<!DOCTYPE html>
<html lang='en'>

<head>
    <title> Index </title>
</head>

<body>
    <div id='question'>
        {% for post in all_posts %}
            {{ post }}
        {% endfor %}
    </div>
</body>
</html>

Next >> Django Tutorial : Creating Migrations

Leave a Reply

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