Python Django - 您是否忘记注册或加载此标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35054230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - Did you forget to register or load this tag?
提问by 123
I've created a custom tag that I want to use, but Django can't seem to find it. My templatetags
directory is set up like this:
我创建了一个想要使用的自定义标签,但 Django 似乎找不到它。我的templatetags
目录是这样设置的:
pygmentize.py
pygmentize.py
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from django import template
from pygments.formatters.other import NullFormatter
register = template.Library()
@register.tag(name='code')
def do_code(parser,token):
code = token.split_contents()[-1]
nodelist = parser.parse(('endcode',))
parser.delete_first_token()
return CodeNode(code,nodelist)
class CodeNode(template.Node):
def __init__(self,lang,code):
self.lang = lang
self.nodelist = code
def render(self,context):
code = self.nodelist.render(context)
lexer = get_lexer_by_name('python')
return highlight(code,lexer,NullFormatter())
I am trying to use this tag to render code in gameprofile.html
.
我正在尝试使用此标签在gameprofile.html
.
gameprofile.html
游戏简介.html
(% load pygmentize %}
{% block content %}
<title>{% block title %} | {{ game.title }}{% endblock %}</title>
<div id="gamecodecontainer">
{% code %}
{{game.code}}
{% endcode %}
</div>
{% endblock content %}
When I navigate to gameprofile.html
, I get an error:
当我导航到 时gameprofile.html
,出现错误:
Invalid block tag on line 23: 'code', expected 'endblock'. Did you forget to register or load this tag?
第 23 行的块标记无效:“code”,预期为“endblock”。您是否忘记注册或加载此标签?
回答by Ahmed Hosny
The app that contains the custom tags must be in INSTALLED_APPS
. So Are you sure that your directory is in INSTALLED_APPS
?
包含自定义标签的应用程序必须在INSTALLED_APPS
. 所以你确定你的目录在INSTALLED_APPS
?
From the documentation:
从文档:
The app that contains the custom tags must be in
INSTALLED_APPS
in order for the{% load %}
tag to work. This is a security feature: It allows you to host Python code for many template libraries on a single host machine without enabling access to all of them for every Django installation.
包含自定义标签的应用程序必须在
INSTALLED_APPS
,以便{% load %}
标签工作。这是一项安全功能:它允许您在单个主机上托管许多模板库的 Python 代码,而无需为每个 Django 安装启用对所有模板库的访问。
回答by losee
did you try this
你试过这个吗
{% load games_tags %}
at the top instead of pygmentize?
在顶部而不是 pygmentize?
回答by Erick M
The error is in this line: (% load pygmentize %}
, an invalid tag.
Change it to {% load pygmentize %}
错误在这一行:(% load pygmentize %}
,一个无效的标签。将其更改为{% load pygmentize %}
回答by MikeyE
I had the same problem, here's how I solved it. Following the first section of this very excellent Django tutorial, I did the following:
我遇到了同样的问题,这是我解决的方法。在这个非常优秀的 Django 教程的第一部分之后,我做了以下事情:
- Create a new Django app by executing:
python manage.py startapp new_app
- Edit the
settings.py
file, adding the following to the list ofINSTALLED_APPS
:'new_app',
- Add a new module to the
new_app
package namednew_app_tags
. - In a Django HTML template, add the following to the top of the file, but after
{% extends 'base_template_name.html' %}
:{% load new_app_tags %}
- In the
new_app_tags
module file, create a custom template tag (see below). - In the same Django HTML template, from step 4 above, use your shiney new custom tag like so:
{% multiply_by_two | "5.0" %}
- Celebrate!
- 通过执行以下命令创建一个新的 Django 应用程序:
python manage.py startapp new_app
- 编辑
settings.py
文件,将以下内容添加到列表中INSTALLED_APPS
:'new_app',
- 将一个新模块添加到
new_app
名为new_app_tags
. - 在 Django HTML 模板中,将以下内容添加到文件的顶部,但在 之后
{% extends 'base_template_name.html' %}
:{% load new_app_tags %}
- 在
new_app_tags
模块文件中,创建自定义模板标记(见下文)。 - 在同一个 Django HTML 模板中,从上面的第 4 步开始,像这样使用你的闪亮的新自定义标签:
{% multiply_by_two | "5.0" %}
- 庆祝!
Example from step 5 above:
上面第 5 步的示例:
from django import template
register = template.Library()
@register.simple_tag
def multiply_by_two(value):
return float(value) * 2.0
回答by Rahul Nama
You need to change:
你需要改变:
{% endblock content %}
to
到
{% endblock %}
回答by Pardhu Saradhi
In gameprofile.html
please change the tag {% endblock content %}
to {% endblock %}
then it works otherwise django will not load the endblockand give error.
在gameprofile.html
请将标签更改{% endblock content %}
为{% endblock %}
然后它会工作,否则 django 将不会加载endblock并给出错误。
回答by Thomas John
{% load static %}
Please add this template tag on top of the HTML or base HTML file
请将此模板标记添加到 HTML 或基本 HTML 文件的顶部
回答by Abhishek mishra
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
in your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE
.
在您的模板中,使用静态模板标记使用配置的STATICFILES_STORAGE
.
回答by vipmaa
For Django 2.2 + you have to load staticfilesin html template first before use statickeyword
对于 Django 2.2 +,您必须先在 html 模板中加载静态文件,然后才能使用static关键字
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
Also you have to check that you defined STATIC_URLin setting.py
您还必须检查您是否在setting.py 中定义了STATIC_URL
At last make sure the static files exist in defined folder
最后确保静态文件存在于定义的文件夹中