Django: TemplateSyntaxError – Could not parse the remainder

Question:

Exception Value: Could not parse the remainder: '['image/jpeg',' from '['image/jpeg','

Why am I getting this TemplateSyntaxError for the code below?

{% for file in resource.files.all %}

        {% if file.file.content_type|lower in ['image/jpeg', 'image/png', 'image/gif'] %}
          <a class="resource-image" title="{{ file.id }}" href="{{ file.file.url }}">
            <img width="100%" src="{{ file.file.url }}" alt="{{ file.id }} Image"/>
          </a>

        {% elif file.file.content_type|lower in ['video/mp4', 'video/wmv', 'video/mkv', 'video/x-flv', 'video/webm', 'video/mpeg'] %}
          <video width="100%" controls>
            <source src="{{ file.file.url }}">
          </video>

        {% elif file.file.content_type|lower in ['audio/mp4', 'audio/m4a', 'audio/wav', 'audio/x-wav', 'audio/ogg'] %}
          <audio width="100%" controls>
            <source src="{{ file.file.url }}">
          </audio>

        {% elif file.file.content_type|lower in ['application/pdf'] %}
          <!-- <embed src="{{ file.file.url }}" width="800px" height="2100px" /> -->

        {% endif %}

        <p class="small text-muted">{{ file.id }}</p>

{% endfor %}

I’ve checked the entire template file, and all the brackets are balanced. I can’t find any syntax errors.

Asked By: Aminul Islam

||

Answers:

You cannot create lists, or tuples, within the template system. Declare in Python and send them as iterators in render() function.

from django.shortcuts import render

def my_view(request):
    list_types = ['image/jpeg', 'image/png', 'image/gif']
    return render(request, 'index.html', {
        'list_types': list_types,
    })
Answered By: Andros Fenollosa