简体   繁体   中英

[Django][MultipleChoiceField] Passing checkboxes values to server

could you let me know how to pass my checkbox values to server?

views.py

def viewtest(request):
    if request.method == "POST":
        form=testForm(request.POST)
        if(form.is_valid():
            form.save()
    elif request.method == "GET":
        form=testForm()
    return render(request,"test/register.html",{"testform":form})

forms.py

class testForm(forms.ModelForm):
    workday=forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), choices=workdayType)
    class Meta:
        model=test
        fields=('name','tel')

register.html

 <table>
    <form method="post" action="{% url "viewtest" %}">
        {{testform.as_table}}
    </form>
    <input type="submit" value="register"/>
  </table>

I can see like below checkboxes.
在此处输入图片说明

but at server, can't see multiple data. could you provide me some idea?

there was my silly stuff. because I've used getting POST data as request.POST['workday']. it was wrong. I SHOULD USE form.cleaned_data.get("workday"). that's for my solution. But don't know what is different yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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