You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django TemplateSyntaxError:无法解析剩余内容问题求助

Django TemplateSyntaxError 问题解决

错误原因

你遇到的Could not parse the remainder: '['image/jpeg',' from '['image/jpeg',''错误,核心原因是Django模板语法不支持直接在in操作符后使用Python列表字面量。模板引擎无法识别['image/jpeg', ...]这种写法,会将其判定为无效的模板语法片段。

解决方案

方法1:视图层传递类型列表(推荐)

在视图函数中定义需要判断的内容类型列表,再传递到模板中使用:

# 视图代码示例
image_types = ['image/jpeg', 'image/png', 'image/gif']
video_types = ['video/mp4', 'video/wmv', 'video/mkv', 'video/x-flv', 'video/webm', 'video/mpeg']
audio_types = ['audio/mp4', 'audio/m4a', 'audio/wav', 'audio/x-wav', 'audio/ogg']
pdf_types = ['application/pdf']

return render(request, '你的模板文件名.html', {
    'resource': resource,
    'image_types': image_types,
    'video_types': video_types,
    'audio_types': audio_types,
    'pdf_types': pdf_types
})

修改模板代码,直接引用视图传递的变量:

{% for file in resource.files.all %}
    {% if file.file.content_type|lower in image_types %}
        <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_types %}
        <video width="100%" controls>
            <source src="{{ file.file.url }}">
        </video>
    {% elif file.file.content_type|lower in audio_types %}
        <audio width="100%" controls>
            <source src="{{ file.file.url }}">
        </audio>
    {% elif file.file.content_type|lower in pdf_types %}
        <!-- <embed src="{{ file.file.url }}" width="800px" height="2100px" /> -->
    {% endif %}
    <p class="small text-muted">{{ file.id }}</p>
{% endfor %}

方法2:模板内用split模拟列表(适用于Django 3.2+)

如果不想修改视图,可以在模板中通过with标签结合split过滤器,将字符串分割为列表:

{% with image_types='image/jpeg,image/png,image/gif'|split:',' %}
{% with video_types='video/mp4,video/wmv,video/mkv,video/x-flv,video/webm,video/mpeg'|split:',' %}
{% with audio_types='audio/mp4,audio/m4a,audio/wav,audio/x-wav,audio/ogg'|split:',' %}
{% with pdf_types='application/pdf'|split:',' %}

{% for file in resource.files.all %}
    {% if file.file.content_type|lower in image_types %}
        <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_types %}
        <video width="100%" controls>
            <source src="{{ file.file.url }}">
        </video>
    {% elif file.file.content_type|lower in audio_types %}
        <audio width="100%" controls>
            <source src="{{ file.file.url }}">
        </audio>
    {% elif file.file.content_type|lower in pdf_types %}
        <!-- <embed src="{{ file.file.url }}" width="800px" height="2100px" /> -->
    {% endif %}
    <p class="small text-muted">{{ file.id }}</p>
{% endfor %}

{% endwith %}{% endwith %}{% endwith %}{% endwith %}

补充说明

你之前检查括号平衡没问题,但问题根源不在括号,而是Django模板引擎的语法限制——它不支持像Python那样直接在模板中写列表字面量来做in判断,必须通过视图传递变量或用过滤器转换的方式实现。

内容的提问来源于stack exchange,提问作者Aminul Islam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 02:00:38