Django:TemplateSyntaxError - 无法解析其余部分

问题描述 投票:0回答:2

异常值:

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 html django django-templates syntax-error
2个回答
2
投票

您无法在模板系统中创建列表或元组。在 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,
    })

0
投票

就我而言,是使用

{% if perms.foo %}

并且不使用

用户.perm(foo)

© www.soinside.com 2019 - 2024. All rights reserved.