Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django forms - selected option based on the get request url parameter

I have a form that looks like this - a form that has a drop down select:

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)

I would like to read from request.GET.get('subject') so that I can dynamically select the choices

so it would look something like this:

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

Obviously the choices are: hi, restaurant, event or promotion and the url looking something like this:

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

problem is I cannot do the request.GET.get('subject')

How can we solve this?

like image 951
jonprasetyo Avatar asked Sep 05 '25 17:09

jonprasetyo


1 Answers

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

Django form initial.

like image 191
Shang Wang Avatar answered Sep 08 '25 11:09

Shang Wang