本文介绍了Django表单+复选框列表+单选按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道如何更改此复选框列表代码:

Do You know how to change this list of checkboxes code:

<p>How did you reach this site? <select name="howreach">
<option value="0" selected="selected">Choose one...</option>
<option value="1">Typed the URL directly</option>
<option value="2">Site is bookmarked</option>
<option value="3">A search engine</option>
<option value="4">A link from another site</option>
<option value="5">From a book</option>
<option value="6">Other</option>
</select></p>

到django表单?

如何将此更改为Django表单中的单选按钮列表?:

And how to change this to the list of radio buttons in Django forms?:

Poor <input type="radio" name="rating" value="1" /> 1
<input type="radio" name="rating" value="2" /> 2
<input type="radio" name="rating" value="3" /> 3
<input type="radio" name="rating" value="4" /> 4
<input type="radio" name="rating" value="5" /> 5 Excellent</p>


推荐答案

在您的Python代码中:

In your python code:

class SiteReach(forms.Form):
    howreach = forms.ChoiceField(label = "How did you reach this site?",
                                 choices = HOWREACH_CHOICES,
                                 widget = forms.widgets.CheckboxInput())

必须自己初始化HOWREACH_CHOICES;它是一个元组的列表,(选项值,选项字符串)。

You'll have to initialize HOWREACH_CHOICES on your own; it's a list of tuples, (option value, option string).

您以同样的方式渲染单选按钮:

You render radio buttons in the same way:

class Rating(forms.Form):
    rating = forms.ChoiceField(choices = range(1,6),
                               widget = forms.widgets.RadioSelect())

阅读Widgets文档;这里有一个整体的效用。

Read the documentation on Widgets; there's a whole universe of utility in there.

这篇关于Django表单+复选框列表+单选按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:06