如何在Django中添加列表值?

时间:2019-04-20 10:30:14

标签: django django-forms

如何在Django选择字段中添加列表值?

我的代码是

from django import forms
import os
drives_a = [chr(x) + "," for x in range(65, 90) if os.path.exists(chr(x) + ':')]

class contactForm(forms.Form):
    drives = forms.CharField(label='Drive Name', widget=forms.Select(choices=drives_a))

2 个答案:

答案 0 :(得分:0)

通常会选择

choices = (
    (key1, 'opt1'),
    (key2, 'opt2'),
)

确保此方法有效,然后修改您的drive_a语句以创建它

答案 1 :(得分:0)

如何在标签名称的驱动器值中添加名称

从Django导入表格 导入操作系统,字符串 从django.forms.models导入ModelForm

drives_a = [chr(x)+':',如果os.path.exists(chr(x)+':')表示x在范围(65,90)中

drives_b =(chr(x)+':'如果os.path.exists(chr(x)+':')表示x在范围(65,90)中

CHOICES_DRIVE = [     (“驱动器”,drives_a)

]

class contactForm(forms.Form):     drive = forms.CharField(widget = forms.RadioSelect(choices = CHOICES_DRIVE))