[英]Django custom filter not registering in tag library
I've written the following filter (in templatetags/custom_errors.py
): 我编写了以下过滤器(在
templatetags/custom_errors.py
):
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter
def custom_errors(bound_field):
return mark_safe(f'<span id="{bound_field.id_for_label}-error" class="error">{", ".join(bound_field.errors)}</span>' if bound_field.errors else '')
which I'm trying to use in a template as follows: 我正尝试在模板中使用以下代码:
{% load widget_tweaks %}
{% load custom_errors %}
{% load label_with_classes %}
{% csrf_token %}
<div class="row">
<div class="input-field col s12">
{{ form.session_number|add_error_class:"invalid" }}
{{ form.session_number|custom_errors }}
{{ form.session_number|label_with_classes }}
</div>
</div>
However, I'm getting a TemplateSyntaxError
: 'custom_errors' is not a registered tag library
: 但是,我收到了
TemplateSyntaxError
: 'custom_errors' is not a registered tag library
:
What puzzles me, however, is that some of the tags that are registered, such as label_with_classes
, are done so in the same way. 但是,令我感到困惑的是,某些注册的标签(例如
label_with_classes
)是以相同的方式完成的。 Here is a tree view of dashboard/templatetags
: 这是
dashboard/templatetags
的树状视图:
dashboard/templatetags
├── active_page.py
├── custom_errors.py
├── edit_view_family_heading.py
├── google_analytics.py
├── label_with_classes.py
├── order_link.py
├── paginator.py
└── profile_badge.py
showing that label_with_classes.py
is in the same directory as custom_errors.py
. 表示
label_with_classes.py
是在相同的目录中custom_errors.py
。 Here are the contents of label_with_classes.py
: 以下是
label_with_classes.py
的内容:
from django import template
from django.forms import ChoiceField, BooleanField, DateField
from titlecase import titlecase
register = template.Library()
def pretty_name(name):
"""Convert 'first_name' to 'First Name'."""
return titlecase(name.replace('_', ' '))
@register.filter(is_safe=True)
def label_with_classes(bound_field, contents=None):
'''Extend Django's label_tag() method to provide the appropriate Materialize CSS classes'''
# The field should have the 'active' class if it has a value (see http://materializecss.com/forms.html),
# except for ChoiceFields (which include ModelChoiceFields and MultipleChoiceFields) and BooleanFields
should_be_active = (bound_field.value() and not isinstance(bound_field.field, (ChoiceField, BooleanField)))\
or isinstance(bound_field.field, DateField)
active = 'active' if should_be_active else ''
invalid = 'invalid' if bound_field.errors else ''
classes = f"{active} {invalid}".strip()
contents = contents or pretty_name(bound_field.name)
return bound_field.label_tag(
attrs={'class': classes},
contents=contents,
label_suffix='')
I don't see any difference in the ways both filters are registered (except that one has is_safe=True
and the other doesn't). 我没有看到两个过滤器的注册方式有什么区别(除了一个过滤器具有
is_safe=True
,另一个没有)。 Why is the custom_errors
filter not registering? 为什么
custom_errors
过滤器未注册? Is there some kind of caching issue? 是否存在某种缓存问题?
The terminal from which I was running python manage.py runserver
had somehow become unresponsive and wasn't responding to Cntrl+C. 我运行
python manage.py runserver
的终端以某种方式变得无响应,并且没有响应Cntrl + C。 I terminated the process and restarted the development server, and now things work as expected. 我终止了该过程并重新启动了开发服务器,现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.