Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Template - New Variable Declaration

Let me preface by I am just starting Python so if this is really a simple question ^_^

I have a html file with the following content:

        {%for d in results%}
        <div class="twt">
            <img src="{{ d.profile_image_url }}" width="48px" height="48px" /> <span> {{ d.text }} </span>
            <span class="date">{{ d.created_at }}</span>
        </div>
    {% endfor %}

which works well but I also would like to declare a variable on this page. Let's say for this example, we can it RowNumber which will increment for each d displayed, spitting out the current RowNumber.

I tried doing:

{{ RowNumber = 0}} 
{{ RowNumber ++ }}

But it doesn't seem to allow me to declare RowNumber.

like image 766
TimLeung Avatar asked Apr 05 '09 15:04

TimLeung


People also ask

How do I declare a variable inside a Django template?

Another approach to declare variables in the template is by using custom template tags. Create a custom template tag files named as custom_template_tags.py . Paste the below code in it. Now inside the HTML template use this custom template tag setvar to define a new variable.

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach) This answer is perfect and I learned a great deal!

What are variables in Django templates?

Django templates allow the passing of data from view to template and also provide some limited features of programming such as variables, for loops, etc. Variables in Django are used for passing the data from view to template. In this article, we will learn about variables in Django Templates.

What is DTL (Django template language)?

Django Template Language (DTL) Django’s template engine offers a mini-language to define the user-facing layer of the application. Displaying Variables. A variable looks like this: {{variable}}. The template replaces the variable by the variable sent by the view in the third parameter of the render function.

How to declare variables in a Python template?

Another approach to declare variables in the template is by using custom template tags. Create a custom template tag files named as custom_template_tags.py . Paste the below code in it.

What does a dot mean in a variable in Django?

When the Django template engine encounters a variable, it replaces that variable with the same variable passed in the template engine. An underscore or a dot (but they must not start with a dot or underscore). A dot in a variable name has a special meaning during template rendering.


1 Answers

Check out the documentation on the for loop.

It automatically creates a variable called forloop.counter that holds the current iteration index.

As far as the greater question on how to declare variables, there is no out-of-the-box way of doing this with Django, and it is not considered a missing feature but a feature. If you really wanted to do this it is possible with custom tags but for the most part the philosophy you want to follow is that mostly anything you want to do that would require this should be done in the view and the template should be reserved for very simple logic. For your example of summing up a total, for example, you could use the add filter. Likewise, you can create your own filters just like with tags.

like image 112
Paolo Bergantino Avatar answered Sep 24 '22 15:09

Paolo Bergantino