1

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:

  • January:
  • 01.01.2016 test entry
  • 06.01.2016 on other entry
  • 25.01.2016 last Jan entry
  • February:
  • 03.02.2016 hello

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?

1
  • Can you explain how you are getting the data, and if you are looping through in some way? I've provided a generic answer on setting variables inside django templates, I'll update my answer if I get more information from you. Or someone else will be able to answer better. Commented Jan 26, 2016 at 0:52

1 Answer 1

3

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 %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.