Skip to content Skip to sidebar Skip to footer

Dynamic Session Access In Templates

I'm trying to access session keys within a loop that needs to be dynamic, I think you'll get what I'm going for by looking at my code that isn't working. {% for q in questions %} &

Solution 1:

The syntax of Django templates is very limiting in order to prevent people from putting too much logic inside templates and doesn't allow you to pass parameters to methods.

You can prepare a list of tuples already in the view or write a simple template tag for that. The first options is usually easier:

In the view:

questions = [(q, request.session.get(str(q.id), False)) for q in questions]

In the template:

{% for q, has_voted in questions %}
...
{% endfor %}

Post a Comment for "Dynamic Session Access In Templates"