如何在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))
选择通常是布局的
choices = (
(key1, 'opt1'),
(key2, 'opt2'),
)
确保工作然后修改你的drive_a
语句来创建它
来自django import forms import os,来自django.forms.models的字符串导入ModelForm
drives_a = [chr(x)+':'for x in range(65,90)if os.path.exists(chr(x)+':')]
CHOICES_DRIVE = [(“Drive”,drives_a),
]
class contactForm(forms.Form):drive = forms.CharField(widget = forms.RadioSelect(choices = CHOICES_DRIVE))