相关疑难解决方法(0)

默认情况下为所有视图加载Django模板标记库

我有一个小的排版相关的模板标签库,几乎每个页面都使用它.现在我需要为每个模板加载它

{% load nbsp %}
Run Code Online (Sandbox Code Playgroud)

有没有办法一次性"全局"加载所有视图和模板?将加载标记放入基本模板不起作用.

django django-templates

49
推荐指数
4
解决办法
1万
查看次数

django:过滤器无效

我有一个文章应用程序并尝试制作自定义过滤器,我在文章应用程序中有一个名为templatetags的目录,以及该目录中的tags.py,这是目录结构.

-manage.py(f)
-settings.py(f)
-articles(d)
 - templatetags(d)
  - tags.py(f)
Run Code Online (Sandbox Code Playgroud)

在模板上,文章有自己的目录,所有文章模板都从base.html模板扩展,这里是模板结构.

-base.html(f)
-articles(d)
 -index.html(f)
Run Code Online (Sandbox Code Playgroud)

我在base.html {%load tags%}中加载了代码并使用index.html中的自定义过滤器,并获得了无效的过滤器错误.

tags.py

from django import template                                                                                                                                                        
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def space2Dash(s):
    return s.replace(' ', '_');
Run Code Online (Sandbox Code Playgroud)

我只是无法弄清楚我做错了什么.

编辑:我更改了过滤器名称,abcfilter.py并在我的文件中加载了文章应用程序settings.py

文章/ index.html的

 {% load abcfilter %}
 {{ "foo bar"|space2dash }}
Run Code Online (Sandbox Code Playgroud)

错误:

Request Method: GET
Request URL:    http://localhost:8080/articles/
Django Version: 1.2.5
Exception Type: TemplateSyntaxError
Exception Value:    
Invalid filter: 'space2dash'
Exception Location: ***/lib/python2.7/django/template/__init__.py in find_filter, line 363
Python Executable:  /usr/local/bin/python
Python Version: …
Run Code Online (Sandbox Code Playgroud)

django django-template-filters

20
推荐指数
3
解决办法
4万
查看次数