英文:
Django: TemplateSyntaxError - 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 %}
我已检查整个模板文件,所有括号都是平衡的。我找不到任何语法错误。
英文:
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.
答案1
得分: 2
从模板系统内部不能创建列表或元组。在Python中声明它们,然后作为迭代器在`render()`函数中传递。
```python
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,
})
英文:
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,
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论