I am currently writing a few custom template tags but for some reason they will not load. My directory structure is as follows:
我目前正在编写一些自定义模板标记,但是出于某种原因它们不会加载。我的目录结构如下:
MyProj
|
----MyApp
|
|----templatetags
|
|----myapp_tags.py
|----__init__.py
In myapp_tags.py
在myapp_tags.py
from django.template import Library, Node
from myproj.myapp.models import Product
register = Library()
class LatestProductsNode(Node):
def render(self, context):
context['recent_products'] = Product.objects.all()[:5]
return ''
def get_latest_products(parser, token):
return LatestProductsNode()
get_latest_products = register.tag(get_latest_products)
In settings.py
在settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'myproj.myapp',
)
In the Template
模板中
{% load myapp_tags %}
The error i get when trying to load the page:
当我试图加载页面时得到的错误:
Exception Type: TemplateSyntaxError Exception Value:
'myapp_tags' is not a valid tag library: Could not load template library from django.templatetags.myapp_tags, No module named myapp_tags
4 个解决方案
#1
15
in settings.py, you should never name the project 'myproj' explicitely. In INSTALLED_APPS
, just use 'myapp'. Also, you should have this :
在设置。py,你不应该明确地给这个项目命名为myproj。在INSTALLED_APPS中,只需使用“myapp”。另外,你应该有以下几点:
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.load_template_source',
)
And be sure to have an __init__.py
in the myapp
folder as well as in templatetags
.
一定要有一份简历。py在myapp文件夹中,也在templatetags中。
Use manage.py shell
then from myapp.templatetags import myapp_tags
to find out if theres any python error in the myapp_tags.py file.
使用管理。然后来自myapp。templatetags导入myapp_tags以查明myapp_tags中是否存在任何python错误。py文件。
Also, be sure that myapp_tags.py file name doesnt conflicts with another folder/file in your project.
另外,请确保myapp_tags。py文件名与项目中的另一个文件夹/文件没有冲突。
Hope this helps.
希望这个有帮助。
#2
14
One thing that's tripped me up is that the magic importing of templatetags bypasses the automatic reloading of the development server. If the following works in manage.py shell
有一件事让我感到困惑,那就是神奇的templatetags导入绕过了开发服务器的自动重新加载。如果以下内容在管理中有效。py壳
>>> from django.templatetags import myapp_tags
>>>
Then everything is actually working and you just need to reload the development server. If on the other hand you get an ImportError
then something is wrong and you should check your INSTALLED_APPS
, that you have an __init__.py file in the templatetags directory and all the other things suggested in the other answers.
然后,一切都正常工作,您只需重新加载开发服务器。另一方面,如果你遇到紧急情况,那么你应该检查一下你的INSTALLED_APPS,你有一个__init__。templatetags目录中的py文件,以及其他答案中建议的所有其他内容。
This will probably only apply to a tiny fraction of the people who experience template tag loading problems, but this is the second time I've arrived at this question in as many weeks and both times it's just taken restarting the development server to get things working.
这可能只适用于遇到模板标签加载问题的一小部分人,但这是我在几周内第二次遇到这个问题,而且这两次都是重新启动开发服务器以使其正常工作。
#3
2
Some reasons:
一些原因:
- due to error in templatetgs code.
- 由于模板代码中的错误。
- If you have used model import in templatetags
- 如果您在templatetags中使用了模型导入
For #2, for example. If you are doing:
例如,# 2。如果你正在做的事情:
from your_app2.models import model
This will go wrong, so instead above, you should do
这会出错,所以你应该在上面做
from your_project.your_app2.models import model
It worked me this way.
这是我的工作方式。
#4
-3
The problem is that nyapp_tags
is not at the top level of an installed project. If you put myproj.myapp.templatetags
in INSTALLED_APPS
, you should be fine.
问题是,nyapp_tags不在已安装项目的顶层。如果你把myproj.myapp。在INSTALLED_APPS中,应该没问题。
#1
15
in settings.py, you should never name the project 'myproj' explicitely. In INSTALLED_APPS
, just use 'myapp'. Also, you should have this :
在设置。py,你不应该明确地给这个项目命名为myproj。在INSTALLED_APPS中,只需使用“myapp”。另外,你应该有以下几点:
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.load_template_source',
)
And be sure to have an __init__.py
in the myapp
folder as well as in templatetags
.
一定要有一份简历。py在myapp文件夹中,也在templatetags中。
Use manage.py shell
then from myapp.templatetags import myapp_tags
to find out if theres any python error in the myapp_tags.py file.
使用管理。然后来自myapp。templatetags导入myapp_tags以查明myapp_tags中是否存在任何python错误。py文件。
Also, be sure that myapp_tags.py file name doesnt conflicts with another folder/file in your project.
另外,请确保myapp_tags。py文件名与项目中的另一个文件夹/文件没有冲突。
Hope this helps.
希望这个有帮助。
#2
14
One thing that's tripped me up is that the magic importing of templatetags bypasses the automatic reloading of the development server. If the following works in manage.py shell
有一件事让我感到困惑,那就是神奇的templatetags导入绕过了开发服务器的自动重新加载。如果以下内容在管理中有效。py壳
>>> from django.templatetags import myapp_tags
>>>
Then everything is actually working and you just need to reload the development server. If on the other hand you get an ImportError
then something is wrong and you should check your INSTALLED_APPS
, that you have an __init__.py file in the templatetags directory and all the other things suggested in the other answers.
然后,一切都正常工作,您只需重新加载开发服务器。另一方面,如果你遇到紧急情况,那么你应该检查一下你的INSTALLED_APPS,你有一个__init__。templatetags目录中的py文件,以及其他答案中建议的所有其他内容。
This will probably only apply to a tiny fraction of the people who experience template tag loading problems, but this is the second time I've arrived at this question in as many weeks and both times it's just taken restarting the development server to get things working.
这可能只适用于遇到模板标签加载问题的一小部分人,但这是我在几周内第二次遇到这个问题,而且这两次都是重新启动开发服务器以使其正常工作。
#3
2
Some reasons:
一些原因:
- due to error in templatetgs code.
- 由于模板代码中的错误。
- If you have used model import in templatetags
- 如果您在templatetags中使用了模型导入
For #2, for example. If you are doing:
例如,# 2。如果你正在做的事情:
from your_app2.models import model
This will go wrong, so instead above, you should do
这会出错,所以你应该在上面做
from your_project.your_app2.models import model
It worked me this way.
这是我的工作方式。
#4
-3
The problem is that nyapp_tags
is not at the top level of an installed project. If you put myproj.myapp.templatetags
in INSTALLED_APPS
, you should be fine.
问题是,nyapp_tags不在已安装项目的顶层。如果你把myproj.myapp。在INSTALLED_APPS中,应该没问题。