Django Template Tags

  • Django template tags are used to embed dynamic content and control the flow of logic within Django templates. They provide a way to perform operations and manipulate data before displaying it in the final rendered HTML.
  • Template tags are enclosed in curly braces ({% %}) and are used to execute certain actions or display specific content. Here's an example to illustrate how Django template tags work:
  • Let's say you have a Django view that passes a list of products to a template. Each product has a name and a price associated with it. You want to display the products in a table, with the name in one column and the price in another.
  • In your template, you can use a Django template tag called `for` to iterate over the list of products and display them:
  • views.py

    from django.shortcuts import render


    def handleHome(request):
        context = [
            {"name": "Mobile", "price": "25000"},
            {"name": "Mac", "price": "100000"},
        ]
        return render(request, "index.html", {"products": context})

  • index.html

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>

    <body>

        <ul>
            {% for product in products %}
            <li>
                Name: {{ product.name }} | Price: {{ product.price }}
            </li>
            {% endfor %}
        </ul>
    </body>

    </html>


  • In the above example, the `{% for product in products %}` template tag begins the iteration over the `products` list. It loops through each `product` in the list, and the subsequent HTML code within the loop is repeated for each product. Inside the loop, `{{ product.name }}` and `{{ product.price }}` are template variables that display the name and price of each product, respectively.
  • The `{% endfor %}` template tag marks the end of the loop. Once the template is rendered, the table will be populated with the names and prices of the products.
  • Django provides many other template tags for various purposes, such as conditional rendering, variable manipulation, date formatting, and more. They offer flexibility and control when constructing dynamic templates in Django.
  • Django provides a variety of template tags to handle different scenarios and perform various actions.
Here's a list of commonly used template tags available in Django:
  • {% if %}: Conditional statement to control the flow of the template based on a condition.
  • {% for %}: Iterates over a sequence (such as a list or queryset) and repeats a block of code for each item.
  • {% include %}: Includes the contents of another template file within the current template.
  • {% block %} and {% extends %}: Used for template inheritance. `{% block %}` defines a block of content that can be overridden in child templates, and `{% extends %}` specifies that the template extends a base template.
  • {% url %}: Generates a URL based on the given view name and parameters.
  • {% csrf_token %}: Inserts a CSRF token in forms to protect against Cross-Site Request Forgery attacks.
  • {% with %}: Assigns a value to a template variable within a specific context.
  • {% load %}: Loads a template library or custom template tags.
  • {% static %}: Resolves the URL for a static file, such as CSS or JavaScript.
  • {% trans %} and {% blocktrans %}: Used for translation and internationalization of text within templates.
  • {% comment %}: Allows adding comments within templates that are ignored during rendering.
  • {% autoescape %}: Controls the automatic escaping of HTML entities in the rendered template.
  • {% verbatim %}: Prevents template processing inside a block of code, allowing the content to be displayed as-is.
  • {% ifchanged %}: Compares the value of a variable and executes the block of code only if the value has changed.
  • {% regroup %}: Groups a list of objects based on a specified attribute.
  • These are just a few examples of the template tags provided by Django. You can refer to the Django documentation for a complete list of template tags and their usage.

No comments:

Post a Comment