ym-b
ym-b

Reputation: 75

Py/Django: Set variables in template

I try to make a month calendar view with python/Django. Is it possible to set variables in {% %} segments?

I want to have it like this:

The top month names are generated when a new month starts. Is something like this possible?

{% if entry.month == lastmonth %} 
// show month name
{% lastmonth = entry.month %} 

And this in a for structure?

Upvotes: 1

Views: 129

Answers (1)

Raiyan
Raiyan

Reputation: 1687

You can use the {% with %} template tag to set variables inside templates. See here for details: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#with

In your case, you can do something like this:

{% for item in collection %}
    {% if item-satisfies-condition %}
        {% with variable=some-value %}
            stuffs...
        {% endwith %}
    {% endif %}
{% endfor %}

Upvotes: 3

Related Questions