Muhammad Awais
Muhammad Awais

Reputation: 365

what is difference between {{}} and {% %} in django templates

I am very new to django and working on it.. I visited a html file and dont know the difference between {{}} and {% %} in html files used as here

{% load static %}

Thanks a lot

Upvotes: 10

Views: 9816

Answers (5)

Muhammad Abdullah
Muhammad Abdullah

Reputation: 29

{% %} for IF ELSE CONDITIONS and FOR LOOP etc

{{ }} for veriables that rendered from view function also used in FOR LOOP veriables like

         `enter code here`
{% for obj in qs%}  
    {{ obj.veriable_name }}
{% endfor %}

Upvotes: 1

Aditya Gupta
Aditya Gupta

Reputation: 41

There are three things in the template in Django First is template variable and the second thing is template tag and third and last is template filter so we write a template variable is {{}} and write a template tag is {% %} third and last is template filter {{variable |filter:arg}}

Upvotes: 4

Ken
Ken

Reputation: 1404

{% %} is for displaying code and {{}} is for displaying variables

Upvotes: 6

Usman Maqbool
Usman Maqbool

Reputation: 3371

You can use

  • {% %} For sentences such as if and for or to call tags such as load, static, etc.

  • {{ }} To render variables in the template.

Read More about it at Django Docs

Upvotes: 16

Everton
Everton

Reputation: 21

I'm new too for Django, so if i'm wrong, please someone correct me. The difference between they are:

{{variable}} is used to use a variables. When the template encounters a variable, it evaluates that variable and replaces it with the result.

You also can use filters {{variable|filter}} like this: {{name|length}} in this case you will use a variable "name" and return the length of that variable.

{%tag%} could use for loops or logic, or load external information into the template to be used by later variables. You can create block tags to help extend other html files parts. Also you can create custom tags.

A good place to see how to do it: https://www.codementor.io/hiteshgarg14/creating-custom-template-tags-in-django-application-58wvmqm5f

Tags like loops and block, need to be closed.

Upvotes: 2

Related Questions