Django选择选项 [英] Django select options

查看:194
本文介绍了Django选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个具有文件名字段,上传文件字段和选择的应用程序。让我说我有这样的选择

 < select name =menu> 
< option value =0selected>选择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 =submitvalue =上传/>

我有文件上传工作与这个课程




$ b title = forms.CharField(max_length = 50)
file = forms.FileField(widget = forms.FileInput( ))

应该怎么样,该类的外观与添加的选择?
或者如何使用文件上传表单并从select中获取值,并根据该值执行操作?

解决方案

您需要使用 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)


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)

这篇关于Django选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆