10

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

2

5 Answers 5

16

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

1
  • 2
    someone is not feeling cheer in the new year
    – Jason
    Jan 1, 2018 at 14:31
6

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

4

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}}

2

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.

1

{% %} 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 %}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.