1

I have a context dictionary containing data to be passed into the template. I need to toggle two divs based on some flag variable which I have implemented using {% with %} Django template tag. However, when I try to set the variable using {% set %} syntax I am getting following error:- set', expected 'endwith'. Did you forget to register or load this tag?

I following the solution given here but it gives me error.

index.html

{% with flag=1 %}
                {% for benefit in content.benefits %}
                    <div style="background-color: #fff;" class="row mt-5">
                        {% if not flag %}
                            <div class="col-lg-4 col-md-4 col-sm-12">
                                <img src="{% static "{{benefit.image}}" %}" alt="tablet"
                                     class="img-responsive mx-auto mt-5 w-100 h-75 h-md-50 working-img">
                            </div>
                        {% endif %}
                        <div class="col-lg-8 col-md-8 col-sm-12 h-100">
                            {% for desc in benefit.heading %}
                                <div class="d-flex h-25 w-100 m-1 mt-4">
                                    <div class="col-3 col-sm-2 h-100">
                                        <div class="mx-auto">
                                            <i class="fas fa-check fa-2x" style="color: #6fe33d"></i>
                                        </div>
                                    </div>
                                    <div class="col-9 col-sm-10">
                                        <div class="d-flex flex-column">
                                            <div class="working-caption font-weight-bold">{{ desc }}</div>
                                            {#                                            <div class="py-2 working-description-courses text-muted">{{ description }}</div>#}
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                        {% if flag %}
                            <div class="col-lg-4 col-md-4 col-sm-12">
                                <img src="{% static "{{benefit.image}}" %}" alt="tablet"
                                     class="img-responsive mx-auto mt-5 w-100 h-75 h-md-50 working-img">
                            </div>
                        {% endif %}
                    </div>
                {% endfor %}
                {% set flag=1-flag %}
            {% endwith %}

settings.py

...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'environment': 'myapp.jinja2.environment'
        },
    },
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...
Vineet
  • 723
  • 4
  • 12
  • 31
  • There is no tag called `{% set %}` AFAIK – Moha369 Oct 09 '19 at 23:11
  • Are you sure your index.html template is being processed by jinja2? The set tag is jinja2 only. What is the full path to the template? – FlipperPA Oct 10 '19 at 02:13
  • @FlipperPA full path to the template is - ```courses/template/courses/index.html``` – Vineet Oct 10 '19 at 08:13
  • @Moha369 I used ```{% set %}``` since I could not find any similar tag to update ```flag``` variable in ```DjangoTemplates``` – Vineet Oct 10 '19 at 08:15
  • You should see the docs, they used `with` to set variables, check `with` in built-in tags – Moha369 Oct 10 '19 at 08:17
  • @Moha369 I have set variable using ```with``` but I need to update ```flag``` variable inside ```for``` loop which cannot be done using ```with```. I cannot find any tag for updation in docs – Vineet Oct 10 '19 at 08:20
  • A custom filter should do what you want. – Moha369 Oct 10 '19 at 08:24
  • @Moha369 `{% set %}` is a valid tag in Jinja2, and the settings file shows that both template engines are being used. – FlipperPA Oct 10 '19 at 14:25

1 Answers1

1

The command you are trying to use, set, is only available with the Jinja2 template engine, not Django's template engine.

https://jinja.palletsprojects.com/en/2.10.x/templates/#assignments

Since you're using the APP_DIRS convention in both the Jinja2 and Django template engines in your settings, you need to put any Jinja2 templates in courses/jinja2/courses/index.html instead of courses/template/courses/index.html. See here for more details:

https://docs.djangoproject.com/en/2.2/topics/templates/#django.template.backends.jinja2.Jinja2

Good luck!

FlipperPA
  • 13,607
  • 4
  • 39
  • 71