Crispy 表单抛出 VariableDoesNotExist 错误,查找表单上的键 [html5_required] 失败
Crispy forms throws VariableDoesNotExist error failed lookup for key [html5_required] on forms
我使用 allauth 通过电子邮件登录,并做了一个非常基本的自定义登录表单和 allauth 的模板覆盖并显示登录表单。点击 URL 会让我直接进入异常:
Failed lookup for key [html5_required] in [{'True': True, 'False': False, 'None': None}, {'True': True, 'False': False, 'None': None, 'form': , 'form_show_errors': True, 'form_show_labels': True, 'label_class': '', 'field_class': ''}, {'forloop': {'parentloop': {}, 'counter0': 1, 'counter': 2, 'revcounter': 2, 'revcounter0': 1, 'first': False, 'last': False}, 'field': }, {}]
然后我必须继续调试程序两次才能在表单上结束。
我试着寻找这个特定的 [html5_required]
tag/key 但没有找到任何人丢失了同样的钥匙。
我删除了 settings.py
中的自定义登录表单以查看是否存在问题,但没有帮助。
我什至用一个简单的 ```ModelForm`` 测试了它,只显示了两个字段并遇到了同样的问题。
我都试过了:class 基于视图(用于登录)和基于函数的视图(用于配置文件),我在两者上都遇到了同样的问题。
settings.py:
INSTALLED_APPS = [
...
'crispy_forms',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
...
]
CRISPY_TEMPLATE_PACK = 'bootstrap4'
ACCOUNT_FORMS = {
"login": "users.forms.CustomLoginForm"
}
forms.py
from django.utils.translation import ugettext as _
from django.urls import reverse_lazy
from allauth.account.forms import LoginForm, SignupForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML
from django.forms import ModelForm
class CustomLoginForm(LoginForm):
def __init__(self, *args, **kwargs):
super(CustomLoginForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
# Add magic stuff to redirect back.
self.helper.layout.append(
HTML(
"{% if redirect_field_value %}"
"<input type='hidden' name='{{ redirect_field_name }}'"
" value='{{ redirect_field_value }}' />"
"{% endif %}"
)
)
# Add password reset link.
self.helper.layout.append(
HTML(
"<p><a class='button secondaryAction' href={url}>{text}</a></p>".format(
url=reverse_lazy('account_reset_password'),
text=_('Forgot Password?')
)
)
)
# Add submit button like in original form.
self.helper.layout.append(
HTML(
'<button class="btn btn-primary btn-block" type="submit">'
'%s</button>' % _('Sign In')
)
)
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-xs-2 hide'
self.helper.field_class = 'col-xs-8'
templates/account/login.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">Login</button>
</form>
{% endblock %}
以下代码是使用几乎相同模板的配置文件的基于快速功能的视图:
#in urls.py: path('profile/', views.profile_view, name='user_profile')
#forms.py:
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('gender', 'birthdate')
#view.py:
def profile_view(request, *args, **kwargs):
if request.method == "POST":
form = UserProfileForm(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
#profile.author = request.user
#profile.published_date = timezone.now()
profile.save()
# TODO: add message or redirect ?!
else:
form = UserProfileForm()
return render(request, 'profile.html', {'form': form})
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Profile</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">Update</button>
</form>
{% endblock %}
我不明白为什么缺少此密钥,我是不是忘记了什么或错过了配置部分?
我使用的版本:
- django: 2.2.4
- 阿拉斯:0.39.1
- 脆皮形式:1.7.2
- python: 3.7.4
非常感谢任何指导。
编辑:
我创建了一个完整的新项目作为测试,只有脆皮形式,一个只有 2 个字符字段的模型,上面提到的 ModelForm 和模板,并收到同样的问题。
所以要么我正在做某事 wrong/missing 要么有问题,
我还提出了他们的 github 问题 https://github.com/django-crispy-forms/django-crispy-forms/issues/891
我遇到了完全相同的问题。
在您基于功能的视图中(针对配置文件)
#forms.py:
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('gender', 'birthdate')
将以下导入添加到顶部
from crispy_forms.helper import FormHelper
然后在 class 定义之后直接创建助手
#forms.py:
class UserProfileForm(ModelForm):
helper = FormHelper()
class Meta:
model = UserProfile
fields = ('gender', 'birthdate')
我做了上面的操作,它解决了我的问题。
我将其保留为 "partial" 答案,因为我不知道为什么或有什么区别(在行为上),而是将模板中的表单调用从:
{{ form|crispy }}
到
{% crispy form %}
排除了异常。
然后,正如@skilled-in-blockchain 提到的,我可以将 FormHelper
添加到表单中以进行其他修改。谢谢
请允许我总结一下 Github 上的问题。
显然,正如 this respond.
中指出的那样,这是 VS Code 的一个问题
可以通过在 launch.json 文件中设置 "django": false
来解决。但是,这将不再允许您调试模板的呈现。换句话说,在模板中设置断点将不再导致调试器执行在这些点处暂停。有关在 VS Code 中调试 Django 的更多信息,请参阅 this tutorial.
如其他post所述,切换自:
{{ form|crispy }}
至此
{% crispy form %}
也解决了这个问题。但是,这也需要你使用FormHelper to insert buttons, or id's and more. Check out crispy form's documentation on the fundamentels如何使用它。
我使用 allauth 通过电子邮件登录,并做了一个非常基本的自定义登录表单和 allauth 的模板覆盖并显示登录表单。点击 URL 会让我直接进入异常:
Failed lookup for key [html5_required] in [{'True': True, 'False': False, 'None': None}, {'True': True, 'False': False, 'None': None, 'form': , 'form_show_errors': True, 'form_show_labels': True, 'label_class': '', 'field_class': ''}, {'forloop': {'parentloop': {}, 'counter0': 1, 'counter': 2, 'revcounter': 2, 'revcounter0': 1, 'first': False, 'last': False}, 'field': }, {}]
然后我必须继续调试程序两次才能在表单上结束。
我试着寻找这个特定的 [html5_required]
tag/key 但没有找到任何人丢失了同样的钥匙。
我删除了 settings.py
中的自定义登录表单以查看是否存在问题,但没有帮助。
我什至用一个简单的 ```ModelForm`` 测试了它,只显示了两个字段并遇到了同样的问题。
我都试过了:class 基于视图(用于登录)和基于函数的视图(用于配置文件),我在两者上都遇到了同样的问题。
settings.py:
INSTALLED_APPS = [
...
'crispy_forms',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
...
]
CRISPY_TEMPLATE_PACK = 'bootstrap4'
ACCOUNT_FORMS = {
"login": "users.forms.CustomLoginForm"
}
forms.py
from django.utils.translation import ugettext as _
from django.urls import reverse_lazy
from allauth.account.forms import LoginForm, SignupForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML
from django.forms import ModelForm
class CustomLoginForm(LoginForm):
def __init__(self, *args, **kwargs):
super(CustomLoginForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
# Add magic stuff to redirect back.
self.helper.layout.append(
HTML(
"{% if redirect_field_value %}"
"<input type='hidden' name='{{ redirect_field_name }}'"
" value='{{ redirect_field_value }}' />"
"{% endif %}"
)
)
# Add password reset link.
self.helper.layout.append(
HTML(
"<p><a class='button secondaryAction' href={url}>{text}</a></p>".format(
url=reverse_lazy('account_reset_password'),
text=_('Forgot Password?')
)
)
)
# Add submit button like in original form.
self.helper.layout.append(
HTML(
'<button class="btn btn-primary btn-block" type="submit">'
'%s</button>' % _('Sign In')
)
)
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-xs-2 hide'
self.helper.field_class = 'col-xs-8'
templates/account/login.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">Login</button>
</form>
{% endblock %}
以下代码是使用几乎相同模板的配置文件的基于快速功能的视图:
#in urls.py: path('profile/', views.profile_view, name='user_profile')
#forms.py:
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('gender', 'birthdate')
#view.py:
def profile_view(request, *args, **kwargs):
if request.method == "POST":
form = UserProfileForm(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
#profile.author = request.user
#profile.published_date = timezone.now()
profile.save()
# TODO: add message or redirect ?!
else:
form = UserProfileForm()
return render(request, 'profile.html', {'form': form})
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Profile</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">Update</button>
</form>
{% endblock %}
我不明白为什么缺少此密钥,我是不是忘记了什么或错过了配置部分?
我使用的版本:
- django: 2.2.4
- 阿拉斯:0.39.1
- 脆皮形式:1.7.2
- python: 3.7.4
非常感谢任何指导。
编辑: 我创建了一个完整的新项目作为测试,只有脆皮形式,一个只有 2 个字符字段的模型,上面提到的 ModelForm 和模板,并收到同样的问题。
所以要么我正在做某事 wrong/missing 要么有问题, 我还提出了他们的 github 问题 https://github.com/django-crispy-forms/django-crispy-forms/issues/891
我遇到了完全相同的问题。
在您基于功能的视图中(针对配置文件)
#forms.py:
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('gender', 'birthdate')
将以下导入添加到顶部
from crispy_forms.helper import FormHelper
然后在 class 定义之后直接创建助手
#forms.py:
class UserProfileForm(ModelForm):
helper = FormHelper()
class Meta:
model = UserProfile
fields = ('gender', 'birthdate')
我做了上面的操作,它解决了我的问题。
我将其保留为 "partial" 答案,因为我不知道为什么或有什么区别(在行为上),而是将模板中的表单调用从:
{{ form|crispy }}
到
{% crispy form %}
排除了异常。
然后,正如@skilled-in-blockchain 提到的,我可以将 FormHelper
添加到表单中以进行其他修改。谢谢
请允许我总结一下 Github 上的问题。 显然,正如 this respond.
中指出的那样,这是 VS Code 的一个问题可以通过在 launch.json 文件中设置
"django": false
来解决。但是,这将不再允许您调试模板的呈现。换句话说,在模板中设置断点将不再导致调试器执行在这些点处暂停。有关在 VS Code 中调试 Django 的更多信息,请参阅 this tutorial.如其他post所述,切换自:
{{ form|crispy }}
至此
{% crispy form %}
也解决了这个问题。但是,这也需要你使用FormHelper to insert buttons, or id's and more. Check out crispy form's documentation on the fundamentels如何使用它。