Django Template Language
Tags
Tags
for
The for loop over each item in an array for example to display a list of athletes provided in athlete_list:
<ul>
{%for athlete in athlete_list%}
<li>{{athlete.name}}</li>
{%endfor%}
</ul>
{%for athlete in athlete_list%}
<li>{{athlete.name}}</li>
{%endfor%}
</ul>
if ,elif,and else
Evaluates a variable and if that variable is true the contents of the block are displayed
{% if athlete_list %} Number of athletes: {{ athlete_list|length }} {% elif athlete_in_locker_room_list %} Athletes should be out of the locker room soon! {% else %} No athletes. {% endif %}
in the above if athlete_list is not empty the number of athletes will be displayed by the {{athlete_list|length}} variable . Otherwise if athlete_in_locker_room_list is not empty the message "Athletes should be out..." will be displayed if both lists are empty "No athletes " will be displayed
you can also use filters and various operators in the if tag:
you can also use filters and various operators in the if tag:
{% if athlete_list|length > 1 %} Team: {% for athlete in athlete_list %} ... {% endfor %} {% else %} Athlete: {{ athlete_list.0.name }} {% endif %}
most templates will return string so mathematical comparisons using filters will generally not work as you expect Length is an exception
Comments
the comment out part of a line in a template use the comment syntax{# #}
for example this template would render as 'hello':
{#greeting#}hello
a comment can contain any template code invalid or not For example
{# {% if foo %}bar{% else %} #}
This syntax can only be used for single-line comments (no newlines are permitted between the {# and #} delimiters). If you need to comment out a multiline portion of the template, see the comment tag.
Template inheritance
The most powerful and thus the most complex - part of djangos template engine is template inheritance . template inheritance allow you to build a base skeleton template that contains all the common elements of your site and defines block that child templates can override
by using this we can comment out for example
Comments
The method to comment out part of a line in a template we can use {# #}by using this we can comment out for example
{#hello#}greetings
The template would render as 'greeting' a comment can contain any code invalid or not for example
{# {% if foo %}bar{% else %} #}
This syntax can only be used for single line comments (no newlines are permitted between the {#and#} delimiters ) its used only for single line comments but to comment out multiline line comment we use {%comment%}and{%endcomment%} an optional note can be inserted in the first tag this is useful when commenting out code for documentint why the code was disabled .
<p>Rendered text with {{ pub_date|date:"c" }}</p> {% comment "Optional note" %} <p>Commented out text with {{ create_date|date:"c" }}</p> {% endcomment %}
comment tags cannot be nested
its easiest to understand template inheritance y starting with an example
Template inheritance
as inheritance in other languages the template inheritance allows us to build a base skeleton template that contains all the common elements of our site and defines blocks that child templates can overrideits easiest to understand template inheritance y starting with an example
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="style.css"> <title>{% block title %}My amazing site{% endblock %}</title> </head> <body> <div id="sidebar"> {% block sidebar %} <ul> <li><a href="/">Home</a></li> <li><a href="/blog/">Blog</a></li> </ul> {% endblock %} </div> <div id="content"> {% block content %}{% endblock %} </div> </body> </html>
this template will be our base class which is base.html its a simple html skeleton document that you might use for a simple two coloumn page its the job of child templates to fill the empty blocks with content
In this example the block tag defines three blocks that child templates can fill in all the block tag does is to tell the template engine that a child template may override those portions of the template
A child template might look like this :
In this example the block tag defines three blocks that child templates can fill in all the block tag does is to tell the template engine that a child template may override those portions of the template
A child template might look like this :
{% extends "base.html" %}
{% block title %}My amazing blog{% endblock %}
{% block content %}
{% for entry in blog_entries %}
<br />
<h2>
{{ entry.title }}</h2>
{{ entry.body }}<br />
{% endfor %}
{% endblock %}
{% block title %}My amazing blog{% endblock %}
{% block content %}
{% for entry in blog_entries %}
<br />
<h2>
{{ entry.title }}</h2>
{{ entry.body }}<br />
{% endfor %}
{% endblock %}
thanks for this excellent article
ReplyDeletepython Training in Bangalore | Python Training institute in Bangalore
Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this.
ReplyDeleteoneplus mobile service centre
oneplus service center near me
oneplus service
Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
ReplyDeletexiaomi service centre chennai
redmi service center in chennai
mi service center in chennai
Thanks for sharing.It is good to see the greatest details presented in an easy and understandable manner.Keep posting more.
ReplyDeleteWeb Designing Training in Chennai
Web Designing Course in Chennai
Web Designing Training in Bangalore
Web Designing Course in Bangalore
Web Designing Training in Hyderabad
Web Designing Course in Hyderabad
Web Designing Training in Coimbatore
Web Designing Training
Web Designing Online Training
It was wonerful reading your conent. Thankyou very much. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
ReplyDeleteOur Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest
That is a helpful instrument and every individual who utilizes the system and downloads information and riding on the web, needs to utilize this application. Free Malwarebytes License Key
ReplyDeleteOn Sister’s Day, let us recognize the fact that our brothers and sisters can go to any extent to pull us out from trouble Happy Sisters Day Quotes Wishes
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNewspaper interesting federal rock condition beat mother if. Else summer cost paper my break age easy. Race read including recently present tree result.world-news
ReplyDelete