2017-04-18 88 views
0

我能夠在我的Django-allauth註冊表單上獲得noRecaptcha表單,並使用使用脆皮形式的模板。 Using this answerDjango Reoder norecaptcha字段使用脆皮形式註冊allauth

這個問題實際上是關於當表單包含在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')) 

我寧願不寫模板,並希望有一種方法來覆蓋佈局。

回答

0

我在github上的django-crispy上發現了一個報告問題的答案。一個"undocumented" feature(當時)我終於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" %} &raquo;</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 

希望這有助於他人。

0

尋找這個問題的解決方案之前,並用此方法:

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)

的新版本,我不能設置順序不起作用。

+0

我有django-allauth(0.31.0),並且使用你的代碼,在電子郵件(顯示命令:用戶名,電子郵件,recaptcha,password1,password2)後,我將recaptcha移動到中間位置。一個改進,但不知道如何強制驗證碼到最後。謝謝 – wedward

相關問題