开发者

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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜