Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django select options

I'm making an app that has a file name field, an upload file field and a select. Lets say I have something like this for the select

<select name="menu">
   <option value="0" selected> select imp </option>
   <option value="1"> imp 1 </option>
   <option value="2"> imp 2 </option>
   <option value="3"> imp 3 </option>
   <option value="4"> imp 4 </option>
</select>
<input type="submit" value="Upload" />

I have the file upload working with this class

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file  = forms.FileField(widget=forms.FileInput())

How should the class look with the select added to it? Or how can I use the file upload form and get the value from the select, and based on that value do an action?

like image 475
void Avatar asked Aug 11 '10 23:08

void


1 Answers

You need to use a ChoiceField:

IMP_CHOICES = (
    ('1', 'imp 1'),
    ('2', 'imp 2'),
    ('3', 'imp 3'),
    ('4', 'imp 4'),
)

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file  = forms.FileField(widget=forms.FileInput())
    imp = forms.ChoiceField(choices=IMP_CHOICES)
like image 126
ars Avatar answered Sep 21 '22 01:09

ars