为什么 Django Crispy Forms 会抛出 "module 'django.forms.forms' has no attribute 'BoundField'"
Why is Django Crispy Forms throwing "module 'django.forms.forms' has no attribute 'BoundField'"
当我在 form/fields 上使用“|crispy”或“|as_crispy_field”过滤器时,我得到一个错误,指出该字段没有属性 BoundField。
这工作正常,但我更新了 django/crispy 表格,我不确定我是否错过了一个技巧?表格在没有过滤器的情况下工作正常。
编辑:我正在使用 Django 3.1.2 和 django-crispy-forms 1.8.1。
forms.py:
from django import forms
from django.utils import timezone
from bootstrap_modal_forms.forms import BSModalForm
from backgammon import models
class MatchForm(BSModalForm):
date_played = forms.DateField(initial=timezone.now)
class Meta:
model = models.Match
fields = [
'date_played',
'winner',
'score'
]
views.py
from django.contrib.auth.mixins import PermissionRequiredMixin
from bootstrap_modal_forms.generic import BSModalCreateView
from .forms import MatchForm
class MatchCreate(PermissionRequiredMixin, BSModalCreateView):
permission_required = 'backgammon.add_match'
template_name = 'backgammon/match_form.html'
form_class = MatchForm
success_message = 'Match saved.'
success_url = reverse_lazy('backgammon-index')
match_form.html
{% load crispy_forms_tags %}
<div class="container bg-light">
<form method="post">
{% csrf_token %}
<div class="modal-body">
{% for field in form %}
<div class="form-group{% if field.errors %} invalid{% endif %}">
{{ field|as_crispy_field }}
</div>
{% endfor %}
</div>
<div class="modal-footer">
{% if object %}<a class="btn btn-danger mr-auto" href="{% url 'match-delete' pk=object.pk %}">Delete</a>{% endif %}
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="submit-btn btn btn-primary">Save</button>
</div>
</form>
</div>
回溯:
Traceback (most recent call last):
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\core\handlers\base.py", line 202, in _get_response
response = response.render()
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\response.py", line 83, in rendered_content
return template.render(context, self._request)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 698, in resolve
new_obj = func(obj, *arg_vals)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py", line 98, in as_crispy_field
if not isinstance(field, forms.BoundField) and settings.DEBUG:
Exception Type: AttributeError at /backgammon/match-create/
Exception Value: module 'django.forms.forms' has no attribute 'BoundField'
局部变量将“字段”变量描述为
没有 crispy 过滤器,页面加载正确:
正如@ArakkalAbu 评论的那样,这是一个版本可比性问题。 conda forge 中 django-crispy-forms 的版本已过时,与最新版本的 Django 不兼容。
我通过安装最新版本 1.11.0 解决了这个问题:
https://django-crispy-forms.readthedocs.io/en/latest/install.html
pip 安装 django-crispy-forms==1.11.0
当我在 form/fields 上使用“|crispy”或“|as_crispy_field”过滤器时,我得到一个错误,指出该字段没有属性 BoundField。
这工作正常,但我更新了 django/crispy 表格,我不确定我是否错过了一个技巧?表格在没有过滤器的情况下工作正常。
编辑:我正在使用 Django 3.1.2 和 django-crispy-forms 1.8.1。
forms.py:
from django import forms
from django.utils import timezone
from bootstrap_modal_forms.forms import BSModalForm
from backgammon import models
class MatchForm(BSModalForm):
date_played = forms.DateField(initial=timezone.now)
class Meta:
model = models.Match
fields = [
'date_played',
'winner',
'score'
]
views.py
from django.contrib.auth.mixins import PermissionRequiredMixin
from bootstrap_modal_forms.generic import BSModalCreateView
from .forms import MatchForm
class MatchCreate(PermissionRequiredMixin, BSModalCreateView):
permission_required = 'backgammon.add_match'
template_name = 'backgammon/match_form.html'
form_class = MatchForm
success_message = 'Match saved.'
success_url = reverse_lazy('backgammon-index')
match_form.html
{% load crispy_forms_tags %}
<div class="container bg-light">
<form method="post">
{% csrf_token %}
<div class="modal-body">
{% for field in form %}
<div class="form-group{% if field.errors %} invalid{% endif %}">
{{ field|as_crispy_field }}
</div>
{% endfor %}
</div>
<div class="modal-footer">
{% if object %}<a class="btn btn-danger mr-auto" href="{% url 'match-delete' pk=object.pk %}">Delete</a>{% endif %}
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="submit-btn btn btn-primary">Save</button>
</div>
</form>
</div>
回溯:
Traceback (most recent call last):
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\core\handlers\base.py", line 202, in _get_response
response = response.render()
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\response.py", line 83, in rendered_content
return template.render(context, self._request)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 698, in resolve
new_obj = func(obj, *arg_vals)
File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py", line 98, in as_crispy_field
if not isinstance(field, forms.BoundField) and settings.DEBUG:
Exception Type: AttributeError at /backgammon/match-create/
Exception Value: module 'django.forms.forms' has no attribute 'BoundField'
局部变量将“字段”变量描述为
没有 crispy 过滤器,页面加载正确:
正如@ArakkalAbu 评论的那样,这是一个版本可比性问题。 conda forge 中 django-crispy-forms 的版本已过时,与最新版本的 Django 不兼容。
我通过安装最新版本 1.11.0 解决了这个问题: https://django-crispy-forms.readthedocs.io/en/latest/install.html pip 安装 django-crispy-forms==1.11.0