[英]How to create a custom django filter tag
I am having trouble in getting my site to recognise custom template tags. 我无法让我的网站识别自定义模板标签。 I have the following dir structure: 我有以下目录结构:
Then I have added this to the INSTALLED_APPS: 然后我将其添加到INSTALLED_APPS:
INSTALLED_APPS = (
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.sites',
'project_name'
)
I then reference this inside the template like this: 然后我在模板中引用它,如下所示:
{% load getattribute %}
{% for header in headers %}
<td>{{ obj|getattribute:header }}</td>
{% endfor %}
The error which I get is as follows: 我得到的错误如下:
Could not import controllers.EventController. 无法导入controllers.EventController。 Error was: No module named project_name 错误是:没有名为project_name的模块
Any help would be appreciated for this: 任何帮助将不胜感激:
TIA TIA
Andrew 安德鲁
UPDATE: 更新:
The site works but I cannot get the template tags to work. 该网站有效,但我无法使用模板标签。 If I remove the project_name from the installed_apps I get the following error: 如果我从installed_apps中删除project_name,我会收到以下错误:
Exception Value: 'getattribute' is not a valid tag library: Could not load template library from django.templatetags.getattribute, No module named getattribute 异常值:'getattribute'不是有效的标记库:无法从django.templatetags.getattribute加载模板库,没有名为getattribute的模块
The error is becaus you have wrong your folder's structure, i think you must read the docs, this tutorial (part1) explains the right structure: 错误是因为您的文件夹结构错误,我认为您必须阅读文档,本教程(第1部分)解释了正确的结构:
You have a project that isn't same thing that app: 你有一个与app不同的项目:
And in your INSTALLED_APPS
: 在你的INSTALLED_APPS
:
INSTALLED_APPS = (
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.sites',
'project_name.app_name',
)
That is all 就这些
Ae you sure this is specifically to do with the template tag? 您确定这与模板标签有关吗?
It sounds like the project_name directory is not on your python path. 听起来像project_name目录不在你的python路径上。 The output on the error page should show your current python path, so you can check if it is as expected. 错误页面上的输出应显示当前的python路径,因此您可以检查它是否符合预期。
Read this to learn how to fix it: http://djangotricks.blogspot.com/2008/09/note-on-python-paths.html 阅读本文以了解如何修复它: http : //djangotricks.blogspot.com/2008/09/note-on-python-paths.html
Your project structure is, not to put too fine a point on it, a mess. 你的项目结构,不是太精细,一团糟。 Some of the many things you need to do: 您需要做的许多事情中的一些:
manage.py
and settings.py
should be in the outer level, not inside an application. manage.py
和settings.py
应该在外层,而不是在应用程序内。 views
is - is it actually views.py
? 我不知道第二种views
是什么 - 它实际上是views.py
吗? In which case it will never be used. 在这种情况下,它永远不会被使用。 templatetags
and views
should be __init__.py
, ie two underscores either side. templatetags
和views
的空文件应为__init__.py
,即两侧的两个下划线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.