如何在django中添加列表值?

问题描述 投票:0回答:2

如何在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))
django django-forms
2个回答
0
投票

选择通常是布局的

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

确保工作然后修改你的drive_a语句来创建它


0
投票

How to add name in drive value in label name

来自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)+':')]

drives_b = (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))

© www.soinside.com 2019 - 2024. All rights reserved.