简体   繁体   English

如何创建自定义django过滤器标记

[英]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: 我有以下目录结构:

  • project_name 项目名
    • project_name 项目名
      • templatetags templatetags
        • _ __init __ _.py _ __init __ _.py
        • getattribute.py getattribute.py
      • views 意见
        • _ __init __ _.py _ __init __ _.py
        • index.html 的index.html
      • views 意见
      • settings.py settings.py
      • main.py main.py
      • manage.py manage.py
      • urls.py urls.py
    • nbproject nbproject文件

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不同的项目:

  • project_name 项目名
    • app_name APP_NAME
      • templatetags templatetags
        • getattribute.py getattribute.py
      • models.py models.py
      • views.py views.py
  • settings.py settings.py
  • manage.py manage.py

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: 您需要做的许多事情中的一些:

  • don't use the same name for the containing directory (project) and the inner one (which should be an app name). 不要对包含目录(项目)和内部目录(应该是应用程序名称)使用相同的名称。
  • manage.py and settings.py should be in the outer level, not inside an application. manage.pysettings.py应该在外层,而不是在应用程序内。
  • I don't know what the second views is - is it actually views.py ? 我不知道第二种views是什么 - 它实际上是views.py吗? In which case it will never be used. 在这种情况下,它永远不会被使用。
  • The empty files inside templatetags and views should be __init__.py , ie two underscores either side. templatetagsviews的空文件应为__init__.py ,即两侧的两个下划线。
  • Probably the actual cause of your problem: you need a models.py inside an application, even if it's empty, for Django to load it at all - templatetags won't work without it. 可能是问题的真正原因:你需要在应用程序中使用models.py,即使它是空的,Django也可以加载它 - 如果没有它,templatetags将无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM