2016-06-20 91 views
2

我有一個看起來像這樣的形式 - 有向下選擇一個下降的一種形式:Django表單 - 選擇的選項基於GET請求URL參數

class ContactUs(forms.Form): 

TYPES = (
    ('hi', 'Say Hi'), 
    ('restaurant', 'Introducing a Restaurant'), 
    ('event', 'An Event is coming up'), 
    ('promotion', 'Interesting Promotion'), 
) 
subject = forms.ChoiceField(choices=TYPES) 

我想從request.GET.get('subject')讓我讀可以動態地選擇選擇

所以它會是這個樣子:

subject = forms.ChoiceField(choices=TYPES, initial=request.GET.get('subject')) 

顯然的選擇是:嗨,餐廳,活動或宣傳和URL尋找索姆ething這樣的:

http://localhost:8000/contact?subject=promotion 

問題是我不能做request.GET.get('subject')

我們如何解決這個問題?

回答

2
contact_us = ContactUs(initial={'subject': request.GET.get('subject')}) 

Django form initial

+0

謝謝你 - 我錯過了部分文檔 – jonprasetyo