如何在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))
答案 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)中
CHOICES_DRIVE = [ (“驱动器”,drives_a)
]
class contactForm(forms.Form): drive = forms.CharField(widget = forms.RadioSelect(choices = CHOICES_DRIVE))