2012-01-18 66 views
-1

我正在嘗試使用Django表單創建HTML表單輸入,其中type =「hidden」以及一個自定義的id和class。 期望的結果:使用HiddenInput添加額外的HTML attrs

<input type="hidden" name="techniciansignature" id="techniciansignature" name="techniciansignature" maxlength="255" /> 

django.form.widget Django的源

class HiddenInput(Input): 
    input_type = 'hidden' 
    is_hidden = True 

class TextInput(Input): 
    input_type = 'text' 

forms.py

sign1 = { 
     'id':'estimate_sign_date' 
     } 

sign2 = { 
     'id':'techniciansignature' 
     } 

sign3 = { 
     'id':'approversignature' 
     } 

estimate_sign_date = forms.CharField(required=False, widget=TextInput(attrs=sign1), max_length=255) 
techniciansignature = forms.CharField(required=False, widget=HiddenInput(attrs=sign2), max_length=255) 
approversignature = forms.CharField(required=False, widget=HiddenInput(attrs=sign3), max_length=255) 

源在瀏覽器中查看:

<input type="text" id="estimate_sign_date" name="estimate_sign_date" maxlength="255" /> 
<input type="hidden" name="techniciansignature" /> 
<input type="hidden" name="approversignature" /> 

爲什麼屬性不包含在HiddenInput小部件中,但對於TextInput工作正常?

回答

0

嘗試:

techniciansignature = forms.CharField(required=False, 
             widget=HiddenInput(attrs=sign2), 
             max_length=255, 
             initial='YOUR TEXT') 
+0

問題出在模板中使用{{form.techniciansignature.as_hidden}}覆蓋了窗口小部件。 – 2012-01-18 17:09:29

+0

您應該以這種方式呈現窗體{{form}}或{{form。 as_p}},{{form.as_table}} {{form.as_ul}}。如果你想以其他方式渲染表單,你必須創建一個方法。 {{form.my_render_form}}。你可以在這裏舉一個例子http://tracpub.yaco.es/djangoapps/browser/formadmin/trunk/formadmin/forms.py#L34 – Goin 2012-01-18 17:20:03

0

如果我呈現這種形式:

class F(forms.Form): 
    estimate_sign_date = forms.CharField(required=False, widget=TextInput(attrs=sign1), max_length=255) 
    techniciansignature = forms.CharField(required=False, widget=HiddenInput(attrs=sign2), max_length=255) 

結果是(在Django上(1,3,1, '最終',0):

<label for="estimate_sign_date">Estimate sign date:</label> 
<input type="text" id="estimate_sign_date" name="estimate_sign_date" maxlength="255" /> 
<input type="hidden" id="techniciansignature" name="techniciansignature" /> 
+0

問題是在模板中使用{{form.techniciansignature.as_hidden}}重寫小部件。一旦我想出了這個帖子使其更有用,我該怎麼辦? – 2012-01-18 17:15:28

+0

添加您的問題答案 – Willian 2012-01-18 17:18:09

相關問題