allauth 注册上的 Django Reoder norecaptcha 字段使用脆皮形式
Django Reoder norecaptcha field on allauth signup using crispy forms
我能够在我的 Django-allauth 注册表单上获得 noRecaptcha 表单,模板使用 crispy 表单。 Using this answer
当表单包含在 django-allauth 中并且没有指定布局(根据作者)时,问题实际上是关于在脆皮表单布局上强制字段顺序。因此,当用我自己的自定义表单覆盖注册表单时,我尝试指定布局,但不确定是否能识别它。
结合其他帖子的答案:
from django import forms
from neatf.users.models import User
from nocaptcha_recaptcha.fields import NoReCaptchaField
class AllauthSignupForm(forms.Form):
captcha = NoReCaptchaField()
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'captcha']
field_order = ['username', 'email', 'password1', 'password2',
'captcha']
def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass
我确定我引用了错误的模型,或者它被忽略了。 noRecaptcha 字段正好位于表单的顶部,而不是我希望的底部。
我也试过使用 FormHelper - 但肯定是用错了。
helper = FormHelper()
helper.form_class = '.form-inline'
helper.field_template = 'bootstrap4/layout/inline_field.html'
helper.layout = Layout(
Fieldset('Contact data', 'username', 'email',),
Fieldset('Password', 'password1', 'password2', 'captcha'))
我宁愿不重写模板,希望有一种方法可以覆盖布局。
之前正在寻找解决此问题的方法并使用了此方法:
class AllauthSignupForm(forms.Form):
#https://developers.google.com/recaptcha/docs/display
#https://github.com/praekelt/django-recaptcha
captcha = ReCaptchaField(
attrs={
'theme' : 'clean',
'size' : 'normal', #compact
})
captcha.label = "Капча"
def __init__(self, *args, **kwargs):
super(AllauthSignupForm, self).__init__(*args, **kwargs)
original_fields = self.fields
new_order = OrderedDict()
for key in ['username', 'email', 'password1', 'password2', 'captcha']:
if key in original_fields:
new_order[key] = original_fields[key]
self.fields = new_order
def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass
相关回答:
How does Django Know the Order to Render Form Fields?
How can I order fields in Django ModelForm?
django-allauth: rearrange form fields (change order)
该解决方案适用于 django-allauth==0.29 版本,不适用于新版本
(测试 0.30-0.31)
新版本无法设置顺序
我在 github 中关于 django-crispy 的报告问题中找到了这个问题的答案。 "undocumented" feature (at that time) that I finally found in the docs on tags reference(布局部分未提及)。
非常简单,在模板上完成,需要对 forms.py:
进行零改动
signup.html
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form.username|as_crispy_field }}
{{ form.email|as_crispy_field }}
{{ form.password1|as_crispy_field }}
{{ form.password2|as_crispy_field }}
{{ form.captcha|as_crispy_field }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}
<script src='https://www.google.com/recaptcha/api.js'></script>
<button class="btn btn-primary" type="submit">{% trans "Sign Up" %} »</button>
</form>
forms.py
from django import forms
from nocaptcha_recaptcha.fields import NoReCaptchaField
class AllauthSignupForm(forms.Form):
captcha = NoReCaptchaField()
def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass
希望这对其他人有帮助。
我能够在我的 Django-allauth 注册表单上获得 noRecaptcha 表单,模板使用 crispy 表单。 Using this answer
当表单包含在 django-allauth 中并且没有指定布局(根据作者)时,问题实际上是关于在脆皮表单布局上强制字段顺序。因此,当用我自己的自定义表单覆盖注册表单时,我尝试指定布局,但不确定是否能识别它。
结合其他帖子的答案:
from django import forms
from neatf.users.models import User
from nocaptcha_recaptcha.fields import NoReCaptchaField
class AllauthSignupForm(forms.Form):
captcha = NoReCaptchaField()
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'captcha']
field_order = ['username', 'email', 'password1', 'password2',
'captcha']
def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass
我确定我引用了错误的模型,或者它被忽略了。 noRecaptcha 字段正好位于表单的顶部,而不是我希望的底部。
我也试过使用 FormHelper - 但肯定是用错了。
helper = FormHelper()
helper.form_class = '.form-inline'
helper.field_template = 'bootstrap4/layout/inline_field.html'
helper.layout = Layout(
Fieldset('Contact data', 'username', 'email',),
Fieldset('Password', 'password1', 'password2', 'captcha'))
我宁愿不重写模板,希望有一种方法可以覆盖布局。
之前正在寻找解决此问题的方法并使用了此方法:
class AllauthSignupForm(forms.Form):
#https://developers.google.com/recaptcha/docs/display
#https://github.com/praekelt/django-recaptcha
captcha = ReCaptchaField(
attrs={
'theme' : 'clean',
'size' : 'normal', #compact
})
captcha.label = "Капча"
def __init__(self, *args, **kwargs):
super(AllauthSignupForm, self).__init__(*args, **kwargs)
original_fields = self.fields
new_order = OrderedDict()
for key in ['username', 'email', 'password1', 'password2', 'captcha']:
if key in original_fields:
new_order[key] = original_fields[key]
self.fields = new_order
def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass
相关回答:
How does Django Know the Order to Render Form Fields?
How can I order fields in Django ModelForm?
django-allauth: rearrange form fields (change order)
该解决方案适用于 django-allauth==0.29 版本,不适用于新版本
(测试 0.30-0.31)
新版本无法设置顺序
我在 github 中关于 django-crispy 的报告问题中找到了这个问题的答案。 "undocumented" feature (at that time) that I finally found in the docs on tags reference(布局部分未提及)。
非常简单,在模板上完成,需要对 forms.py:
进行零改动signup.html
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form.username|as_crispy_field }}
{{ form.email|as_crispy_field }}
{{ form.password1|as_crispy_field }}
{{ form.password2|as_crispy_field }}
{{ form.captcha|as_crispy_field }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}
<script src='https://www.google.com/recaptcha/api.js'></script>
<button class="btn btn-primary" type="submit">{% trans "Sign Up" %} »</button>
</form>
forms.py
from django import forms
from nocaptcha_recaptcha.fields import NoReCaptchaField
class AllauthSignupForm(forms.Form):
captcha = NoReCaptchaField()
def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass
希望这对其他人有帮助。