【问题标题】:Django: TemplateSyntaxError - Could not parse the remainderDjango: TemplateSyntaxError - 无法解析余数
【发布时间】:2023-02-08 23:43:54
【问题描述】:

异常值: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2020-06-20
    • 2018-08-31
    • 2011-07-11
    • 2015-03-06
    • 2011-11-06
    • 2018-03-31
    • 2015-03-17
    • 1970-01-01
    相关资源
    最近更新 更多