2015-09-01 61 views
4

我有一個Django脆皮形式:一個典型的註冊形式,包含電子郵件地址,密碼字段和提交動作。 。if語句以Django脆皮形式,條件表單佈局

我已經過了,從我的網址Python文件Django的香脆形式稱爲「billing_secret」的隱藏字段中的計費祕訣是在不同的URL不同

目標: 爲了有一個條款及條件無線電覆選框啓用/禁用特定計費祕密提交按鈕,因此URL。

我需要添加兩件事情。

  1. 脆皮表單中添加一個if語句只顯示了一定的計費SE無線電覆選框CRET。例如,如果計費祕密是「蘋果」顯示無線電並且默認爲「否」。如果計費密碼是隱藏的,則默認爲是。

這是我到目前爲止(不工作)。道歉我對Python完全陌生。

email = forms.EmailField(label=_("Email")) 
password1 = forms.CharField(widget=forms.PasswordInput,label=_("Password")) 
billing_secret = forms.CharField() 
termsandcond = forms.TypedChoiceField(
     label = "Do you agree to the T&C's?", 
     choices = ((1, "Yes"), (0, "No")), 
     coerce = lambda x: bool(int(x)), 
     widget = forms.RadioSelect, 
     initial = '0', 
     required = True, 
    ) 

def __init__(self, *args, **kwargs): 
    billing_secret = kwargs.pop('billing_secret', None) 
    super(RegistrationForm, self).__init__(*args, **kwargs) 
    self.helper = FormHelper() 
    self.helper.form_method = 'post' 
    self.helper.form_action = '.' 

    self.helper.layout = Layout(
     Field('email', placeholder=_("Email")), 
     Field('password1', placeholder=_("Password")), 
     Field('billing_secret', value=billing_secret, type="hidden"), 

     if billing_secret is 'apples': 
      return InlineRadios('termsandcond'), 
     else: 
      return InlineRadios('termsandcond', initial="1", type="hidden"), 

     Submit("save", _("Get Started"),css_class="pull-right"), 
    ) 
  • 禁用當單選按鈕的值是「無」的提交按鈕,並啓用時‘是’
  • 我打算包括這樣:

    http://jsfiddle.net/8YBu5/7/

    這樣,用戶必須同意給T & C'S被允許與BIL如果在提交指定的URL的細節之前報名時靈祕是「蘋果」。如果它們位於不同的URL上,則收音機不存在,並且提交按鈕已啓用。

    回答

    6

    使默認按鈕隱藏:

    Submit("save", _("Get Started"),css_class="pull-right", style='display: none;') 
    

    並與JavaScript的單選按鈕,當接受用戶只需點擊選擇按鈕,顯示它做檢查。

    編輯: 對於條件元素:

    self.helper.layout = Layout(
        Field('email', placeholder=_("Email")), 
        Field('password1', placeholder=_("Password")), 
        Field('billing_secret', value=billing_secret, type="hidden"), 
    ) 
    
    if billing_secret is 'apples': 
        self.helper.layout.append(InlineRadios('termsandcond')) 
    else: 
        self.helper.layout.append(InlineRadios('termsandcond', initial="1", type="hidden")) 
    self.helper.layout.append(Submit("save", _("Get Started"),css_class="pull-right", style='display: none;')) 
    
    +0

    是,但我需要的收音機是唯一的形式爲特定billing_secret。所以我的第一個問題是如何基於billing_secret變量有條件地添加表單元素。一旦我弄清楚,我應該能夠啓用/禁用與JS的按鈕。感謝您的建議。 :) –

    +0

    您可以看到編輯條件元素 – Mounir

    +0

    ,謝謝@mounir。有效。欣賞它! –