django 2.0 自定义simple_tag 错误

作者: print("") 分类: python 发布时间: 2018-07-29 13:07

django 2.0 自定义simple_tag 错误

我的url如下:

urlpatterns = [
    url(r'^tpl1/', views.tpl1),
    url(r'^tpl4/', views.tpl4),
]
def tpl4(request):
    name="ADaihnifa"
    return render(request,"tpl4.html",{'name':name})

tpl4.html 如下:

{% load xxoo %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{ name }}
    {{ name|lower }}
    {{ name|upper  }}
    {% linux2580 %}
</body>
</html>

xxoo.py

from django import template
from django.utils.safestring import mark_safe


register=template.Library()

@register.simple_tag
def linux2580():
    return 123

然后出现

TemplateSyntaxError at /app/

错误 =。=

解决方法如下:

settions.py 

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR),'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
     'libraries':{
                "linux2580":"app01.templatetags.xxoo",
            },
        },
    },
]

linux2580(函数) 是我的自定义标签名字   app01.templateags.xxoo 是我的py的文件路径

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。