sna*_*ile 74 python django portability
我有一个我从朋友那里收到的小Django项目.该代码完美地适用于他的系统.但是,在我的系统上运行服务器时收到以下错误消息:
在/的TemplateSyntaxError
'current_tags'不是有效的标记库:找不到模板库current_tags,尝试过django.templatetags.current_tags
问题是在html文件中有一行:
{% load current_tags %}
Run Code Online (Sandbox Code Playgroud)
这个完全相同的代码在他的系统上运行,没有错误.那可能是什么?
小智 81
我建议如下:
(很可能)您尚未安装标记库的某个依赖项.检查current_tags.py模块内的导入.
确保包含标记库的应用程序在settings.py下面注册INSTALLED_APPS.
确保您可以成功导入标记库.
python manage.py shell
>>> from app.templatetags import current_tags
Run Code Online (Sandbox Code Playgroud)
这归结为以下链接推荐的内容,即错误本身会误导您从哪里寻找模板.它默默地忽略导入时的错误,这意味着current_tags.py它本身可能有语法错误或其引发ImportError的其他原因.
如果其他一切都失败了,请查看以下链接:http: //www.b-list.org/weblog/2007/dec/04/magic-tags/
Ctr*_*l-C 62
可能性很多:
我得到了同样的错误,但出于不同的原因所以我会告诉你(如果其他人遇到同样的问题).
我把一切都搞定了,但是我将自定义标签放在一个名为的文件夹中template_tags,经过长时间的搜索,我发现它必须是templatetags,现在它可以工作了.所以检查文件夹名称是否正确templatetags.
假设您具有以下结构:
- 应用名称
------- templatetags
-------------- init .py
--------------templates_extras.py
------- init .py
-------settings.py
- manage.py
您必须确保以下内容:
您的"templatetags"所在的应用程序本身实际安装在settings.py中的INSTALLED_APPS中(例如"Application_Name")
存在于"templatetags"中的标记模块本身已安装在settings.py中的INSTALLED_APP中(例如"ApplicationName.templatetags.tempaltes_extras")
确保在templatetags目录下有" init .py"
你必须重新启动服务器
在某些情况下,如果不起作用,则必须删除所有生成的*.pyc,然后再次重试
小智 5
"自定义标记"不是有效的标记库错误,更常发生,因为自定义标记未加载到应用程序中.
将一个空的init .py放在放置"自定义模板标记"的同一文件夹中,并在终端上运行以下代码以加载自定义模板标记
touch __init__.py
Run Code Online (Sandbox Code Playgroud)
对于其他面临这个问题的人来说。假设您的应用程序名称是,MyApp并且您的标签文件夹名称是,templatetags那么settings.py您应该有:
INSTALLED_APPS = [
'MyApp',
'MyApp.templatetags'
]
Run Code Online (Sandbox Code Playgroud)
那里需要您的 django 应用程序和应用程序包下的标记文件夹。
-> MyApp
---> models.py
---> views.py
---> templatetags
-----> __init__.py
-----> app_filters.py
Run Code Online (Sandbox Code Playgroud)
在你的模板文件中:
{% load app_filters %}
Run Code Online (Sandbox Code Playgroud)
也app_filters.py可以是这样的:
# coding=utf-8
from django import template
register = template.Library()
@register.filter(name='get_item')
def get_item(dictionary, key):
return dictionary.get(key)
Run Code Online (Sandbox Code Playgroud)
检查以上所有步骤,您可能会发现问题。
| 归档时间: |
|
| 查看次数: |
63151 次 |
| 最近记录: |