2013-05-07 30 views

回答

7

如果您嘗試刪除字段,則可以查看文檔中的Removing Fields Per-instance

從文檔:

有時候,你創造出具有不在 所有情況下,還是對所有用戶有用的字段的表格。雖然確實可以使用 表單繼承來定義一個包含所需字段的表單,但有時需要調整現有表單。幸運的是, 形式可以有場使用德爾 關鍵字刪除,實例化後:

class MagazineIssueForm(Form): 
    title = TextField() 
    year = IntegerField('Year') 
    month = SelectField(choices=MONTHS) 

def edit_issue(): 
    publication = get_something_from_db() 
    form = MagazineIssueForm(...) 

    if publication.frequency == 'annual': 
     del form.month` 
+0

謝謝。但我正在尋找禁用該字段,而不是刪除它。 – sixarm 2013-05-07 18:23:57

+0

對不起 - 我誤解了。你是什​​麼意思禁用?顯示它,但它使該字段不能被修改? – Ewan 2013-05-07 18:27:09

+0

這與另一個問題非常相似,我在那裏寫了一個詳細的答案:http://stackoverflow.com/a/16576294/244393 – Crast 2013-05-15 23:01:37

5

這幾乎就像@Bibhas建議。 如果我理解這個正確的,你想通過HTML disabled屬性來禁用一個字段,那麼下面的工作對我來說:

form.field(disabled=True) 

這個答案可能有點晚了,但如果任何其他人有這個問題,可能有幫助。

3

VIM forms.py:

add_time = DateTimeField(

    '添加時間', 
    format='%Y-%m-%d %H:%M:%S', 
    default=datetime.datetime.now(), 
    # I use bs3,and it well add input an attribute disabled 
    render_kw={'disabled':''}, 
    validators=[DataRequired()], 
) 
+0

謝謝你!在我的例子中,表單已經被實例化,所以我只是'form.my_field.render_kw = {'disabled':'disabled'}'。 – elethan 2017-05-15 17:41:37

相關問題