简体   繁体   English

Django形成多种选择

[英]Django form multiple choice

I am a newbie in Django and I would really appreciate it if you could offer me some guidance. 我是Django的新手,如果你能给我一些指导,我真的很感激。 I am trying to create a form that allows a user to tick one or more options. 我正在尝试创建一个允许用户勾选一个或多个选项的表单。 I understood that I must use MultipleChoiceField field with a CheckboxSelectMultiple widget but the Django documentation doesn't offer an example on this topic. 我知道我必须将MultipleChoiceField字段与CheckboxSelectMultiple小部件一起使用,但Django文档没有提供关于此主题的示例。 I would be grateful if you could offer me an example and explain how do I handle the results. 如果你能给我一个例子并解释我如何处理结果,我将不胜感激。 For example if I have a form with the options abcd, and the user ticks c and d. 例如,如果我有一个带有选项abcd的表单,并且用户勾选c和d。 Also how do I specify the choices(I don't want to use a db, a list of strings is what I have in mind)? 另外我如何指定选项(我不想使用数据库,字符串列表是我的想法)? Thanks a lot 非常感谢

forms.py forms.py

class SomeForm(forms.Form):
    CHOICES = (('a','a'),
               ('b','b'),
               ('c','c'),
               ('d','d'),)
    picked = forms.MultipleChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple())

views.py views.py

def some_view(request):
    if request.method == 'POST':
        form = SomeForm(request.POST)
        if form.is_valid():
            picked = form.cleaned_data.get('picked')
            # do something with your results
    else:
        form = SomeForm

    return render_to_response('some_template.html', {'form':form },
        context_instance=RequestContext(request))

some_template.html some_template.html

<form method='post'>
    {{ form.as_p }}
    <input type='submit' value='submit'>
</form>

results: 结果:

checkboxselectmultiple

explanation: 说明:

choices: 选择:

The first element in each tuple is the actual value to be stored. 每个元组中的第一个元素是要存储的实际值。 The second element is the human-readable name for the option. 第二个元素是选项的可读名称。

getting selected boxes: 获取选中的框:

form.cleaned_data.get('picked') will result in a list of the 'actual values'. form.cleaned_data.get('picked')将生成'实际值'列表。 For example, if I replaced the # do something with your results with print picked you see: 例如,如果我用print picked替换了# do something with your results看到:

[u'a', u'c']

in your console 在你的控制台中

hope this helps :D 希望这会有所帮助:D

from django import forms


class Test(forms.Form):
    OPTIONS = (
        ("a", "A"),
        ("b", "B"),
        )
    name = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                         choices=OPTIONS)

you can check this https://pypi.python.org/pypi/django-multiselectfield/ 你可以查看这个https://pypi.python.org/pypi/django-multiselectfield/

from multiselectfield import MultiSelectField

# ...

MY_CHOICES = (('item_key1', 'Item title 1.1'),
          ('item_key2', 'Item title 1.2'),
          ('item_key3', 'Item title 1.3'),
          ('item_key4', 'Item title 1.4'),
          ('item_key5', 'Item title 1.5'))

MY_CHOICES2 = ((1, 'Item title 2.1'),
           (2, 'Item title 2.2'),
           (3, 'Item title 2.3'),
           (4, 'Item title 2.4'),
           (5, 'Item title 2.5'))

class MyModel(models.Model):

    # .....

    my_field = MultiSelectField(choices=MY_CHOICES)
    my_field2 = MultiSelectField(choices=MY_CHOICES2,
                             max_choices=3,
                             max_length=3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM