0

Like I asked in the title, I wanna do something like the below in Django.

{% for i in "xxxxx" %}
    {% if store{{ forloop.counter }} %}
      ...
    {% endif %}
{% endfor %}

I pass variables named 'store1', 'store2', and 'store3' from views.py However, an error happens saying "Could not parse the remainder: '{{' from 'store{{'" , which seems like {{ }} can't be used inside {% %}

Does anyone know how to do this?

1 Answer 1

4

You can't do this in the Django template language.

A better approach would be to pass the stores to the template as a list,

def my_view(request):
    stores = ['store1', 'store2', ...]
    return render(request, 'mytemplate.html', {'stores': stores}

then loop through the list in the template:

{% for store in stores %}
  {{ store }}
{% 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.