Значение исключения: Could not parse the remainder: '['image/jpeg',' from '['image/jpeg','
Почему я получаю это TemplateSyntaxError
для кода ниже?
{% 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 %}
Я проверил весь файл шаблона, и все скобки сбалансированы. Я не могу найти никаких синтаксических ошибок.
Вы не можете создавать списки или кортежи в системе шаблонов. Объявите в Python и отправьте их как итераторы в функции render()
.
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,
})
Большое спасибо! Собственно, ChatGPT подсказал мне сделать это прямо в шаблоне. Но я использую представление на основе классов. Должен ли я использовать
get_context_data()
, чтобы сделать то же самое?