django表单中的单选button
我有困难的设置forms.py文件,以包括收音机或selectbutton。 我查看了文档,但没有运用正确的语法。
这是我目前在forms.py
–
from django import forms class PictureForm(forms.Form): like = forms.ChoiceField(???) name = forms.CharField() email = forms.EmailField() message = forms.CharField()
并在我的views.py
–
from app.forms import PictureForm def index2(request): if request.method == 'POST': form = PictureForm(request.POST) if form.is_valid(): cd = form.cleaned_data Picture.objects.create(like=cd['like'], name=cd['name'], email=cd['email'], message=cd['message']) return HttpResponseRedirect ('/thanks/') else: form = PictureForm() return render_to_response('index2.html', {'form':form},)
我怎样才能设置一个“value1”,“value2”,“value3”单选button? 如何用select下拉菜单来做到这一点? 谢谢。
编写表单类时,请注意设置字段的小部件和选项。
CHOICES=[('select1','select 1'), ('select2','select 2')] like = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())
默认的小部件是一个下拉select。