0

How can you do the Django template equivalent to this python for loop?

    extra_context = [o.extra_context for o in activities] 

or do I need to register a new template tag?

2 Answers 2

1

If you want to create a new list variable to iterate over, you need access to the template's context - which I don't think you can do from the template itself. As far as I know that must be done from the view. This question may provide more insight. The discussion of template tags found in the same thread is also relevant.

If activities is the only thing being passed through the request context, then I believe the closest thing you can do from within the template without registering a new tag is:

{% for o in activities %}
    {{ o.extra_context }}
    <!-- do stuff -- >
{% endfor %}

I would refer to the above-linked thread for discussion on making a tag that can work with a new list, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

After looking around and reading some documentation, here's my approach

{% for a in activities %}
    {% with a.extra_context as o %}
    {% endwith %}
{% endfor %} 

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.