Chapter 4. Create CRUD Web Application

Django Template Language (DTL)

Django Template Language (DTL)
Tag:

Understanding the Django Template Language (DTL) is important to create Django templates. It serves as a bridge between HTML and Python code, especially for views.py. Using DTL, you can embed variables passed by views.py or add simple logic in an HTML template file.

{{ variable }}

You can display data (objects) passed by views.py using this format.

{{ name|filter }}

Using the filter, you can modify variables for display. For example:

  • {{ name:lower }} displays the value of the {{ name }} variable in lowercase text
  • {{ name|truncatewords:n }} displays the first n words of the {{ name }} variable
  • {{ text|linebreaks }} converts line breaks in {{ text }} variable to <p> tag
  • {{ name:default:"default data" }} displays when the {{ name }} variable is false or empty

{% tag %}

Using this format, you can apply simple logic e.g., for.., if...else.

{#Comments#}

Using this format, you can add comments. The comments won't be displayed in browsers.

Django documentation reference: The Django template language

Understanding the Django Template Language (DTL) is important to create Django templates. It serves as a bridge between HTML and Python code, especially for views.py. Using DTL, you can embed variables passed by views.py or add simple logic in an HTML template file.

{{ variable }}

You can display data (objects) passed by views.py using this format.

{{ name|filter }}

Using the filter, you can modify variables for display. For example:

  • {{ name:lower }} displays the value of the {{ name }} variable in lowercase text
  • {{ name|truncatewords:n }} displays the first n words of the {{ name }} variable
  • {{ text|linebreaks }} converts line breaks in {{ text }} variable to <p> tag
  • {{ name:default:"default data" }} displays when the {{ name }} variable is false or empty

{% tag %}

Using this format, you can apply simple logic e.g., for.., if...else.

{#Comments#}

Using this format, you can add comments. The comments won't be displayed in browsers.

Django documentation reference: The Django template language

Tag: