Django模板过滤器语法错误 [英] Django Template Filter Syntax error

查看:75
本文介绍了Django模板过滤器语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过此代码使用django内置的默认过滤器

I am trying to use django's built in 'default' filter using this code

{% load sekizai_tags static compress i18n %}
[...]
<title>{{ title|default:"nothing" }}</title>

但这给了我以下例外情况

But it gives me the following exception

django.template.base.TemplateSyntaxError: default requires 2 arguments, 1 provided

我正在为模板后端使用以下设置

I am using the following settings for my Template Backend

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {
            'debug': DEBUG,
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'sekizai.context_processors.sekizai',
            ],
        },
    },
]

我的编辑器将代码标记为无效,但我检查了数千次
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/

My editor marks the code as invalid, but i check like a thousand of times https://docs.djangoproject.com/en/1.8/ref/templates/builtins/

在何处给出例如:

{{ value|default:"nothing" }}

我也试图更改标题var,以确保它不是保留的关键字。

I also tried to change the name of title var, to make sure it is not a reserved keyword.

推荐答案

请确保在冒号后没有空格。

Make sure you don't have a space after the colon.

这是正确的:

{{ title|default:"nothing" }}

这引发了异常:

{{ title|default: "nothing" }}

这篇关于Django模板过滤器语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆