Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{% %} and {{ }} in Django

Tags:

I am learning Django and came across 2 sets of special characters that I haven't seen used like this before. I can guess what they are used for in the examples, but don't understand their scope.

They are:

  • {% if registered %}
  • {{ user_form.as_p }}

I added the if registered and user_form.as_p in for context. I am only concerned with the {% %} and {{ }} parts of it.

  • Are these only used in Django or are they also used in Python?
  • What is the meaning of each?
  • Are there other similar sets of characters?
like image 586
pekasus Avatar asked Jan 06 '16 04:01

pekasus


People also ask

What does {{ name }} mean in Django?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is {% block content %} in Django?

Introducing {% block %} The block tag is used to define a block that can be overridden by child templates. In other words, when you define a block in the base template, you're saying that this area will be populated with content from a different, child template file.

What is {{ block super }}?

From the Django documention: "If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it.


2 Answers

These are special tokens that appear in django templates. You can read more about the syntax at the django template language reference in the documentation.

{{ foo }} - this is a placeholder in the template, for the variable foo that is passed to the template from a view.

{% %} - when text is surrounded by these delimiters, it means that there is some special function or code running, and the result of that will be placed here. It is used when the text inside is not passed to the template from the view, but rather a function or feature of the template language itself that is being executed (like a for loop, or an if conditional). You can create your own extensions to the template language, which are called template tags.

{{ foo|something }} - this is yet another syntax you may come across. The |something is a template filter. It is usually for transforming the result of the item on the left of the | symbol. For example {{ foo|title }}.

You can read more about tags and filters which are referred to as template builtins in the documentation.

This syntax is not unique to django - many other template languages in Python (and some outside of Python) have adopted a similar syntax.

The Python language doesn't have the same syntax, but it does have the concept of string templates which is a very simplified version of a template engine.

like image 76
Burhan Khalid Avatar answered Oct 07 '22 19:10

Burhan Khalid


They are used in .html files aka templates. They are not python, they are part of the Django's template engine.

You use {% %} for sentences such as: if and for, or to call tags such as: load, static, etc.

And you use {{ }} to render variables in your template.

like image 36
Gocht Avatar answered Oct 07 '22 18:10

Gocht