我做了一个模板标签,当我在模板中使用它时,出现此错误:
'str'对象没有属性'as_widget'
这是我的模板标签的代码:
from django import template
register = template.Library()
@register.filter
def field_type(bound_field):
return bound_field.field.widget.__class__.__name__
@register.filter
def input_class(bound_field):
css_class = ''
if bound_field.form.is_bound:
if bound_field.errors:
css_class = 'is-invalid'
elif field_type(bound_field) != 'PasswordInput':
css_class = 'is-valid'
return 'form-control {}'.format(css_class)
我的模板在这里:
{% extends 'board/account.html' %}
{% load widget_tweaks %}
{% load form_tags %}
{% block title %}Sign up{% endblock title %}
{% block content %}
<div class="accountform">
<form method="POST">
<h4 class="display-6 text-white p-3">Create your account</h4>
{% csrf_token %}
<div class="alert alert-danger p-0" style="border:none;">
{% for field in form.visible_fields %}
{% for error in field.errors %}
<span class="err">{{ error }}</span><br>
{% endfor %}
{% endfor %}
</div>
<div class="form-group">
<label for="id_username">Username</label>
{% render_field form.username|input_class class='form-control' placeholder='John Doe' %}
</div>
<div class="form-group">
<label for="id_email">Email</label>
{% render_field form.email|input_class class='form-control' placeholder='johndoe@email.com' %}
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="id_password1">Password</label>
{% render_field form.password1|input_class class='form-control' placeholder='****' %}
</div>
<div class="form-group col-md-6">
<label for="id_password2">Confirm Password</label>
{% render_field form.password2|input_class class='form-control' placeholder='****' %}
</div>
</div>
<input type="submit" value="Sign up" class="btn btn-primary btn-block">
<p class="display-6 bg-signup p-3">Have an account <a href="{% url 'signin' %}">Sign In</a></p>
</form>
</div>
</div>
{% endblock content %}
我在SO中看到了类似的问题并获得了批准的答案,基本上说该问题的解决方案是在上下文中添加表单,并且已经添加了,这里是视图
def signup_view(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('board:home')
form = UserRegistrationForm(request.POST)
return render(request, 'accounts/signup.html', {'form': form})
请帮助我解决此问题。谢谢您的帮助。
答案 0 :(得分:1)
似乎没有任何理由使用这两个模板标签。 django-widget-tweaks已经具有add a custom error class with render_field
的功能,并且您已经知道何时渲染密码字段,因此可以在需要时添加该类。
(此外,django-widget-tweaks已经提供了field_type
filter)。
所以:
{% with WIDGET_ERROR_CLASS='my_error' %}
<div class="form-group">
<label for="id_username">Username</label>
{% render_field form.username class+='form-control' placeholder='John Doe' %}
</div>
<div class="form-group">
<label for="id_email">Email</label>
{% render_field form.email class+='form-control' placeholder='johndoe@email.com' %}
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="id_password1">Password</label>
{% render_field form.password1| class+='form-control' placeholder='****' %}
</div>
...
{% endwith %}
答案 1 :(得分:0)
您做错了方法,您必须在类属性中添加calss
而不是将类名呈现为字段。这样做吧。
{% render_field form.username class=form.username|input_class placeholder='John Doe' %}