Django: 模板语法错误 – 无法解析余下的部分

huangapple go评论64阅读模式
英文:

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,
    })

huangapple
  • 本文由 发表于 2023年2月8日 23:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定