我试图从模板中获取用户的输入,我在Django Auth模型中的组模型中显示模板中的组列表,并期望多个值.
from django.contrib.auth.models import Group
class MyForm(forms.ModelForm):
the_choices = forms.ModelMultipleChoiceField(queryset=Group.objects.all(),
required=False,
widget=forms.CheckboxSelectMultiple)
class Meta:
model = Group
exclude = ['name', 'permissions']
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
views.py
from .forms import MyForm
from django.shortcuts import render
from django.views.generic import View
class see(View):
def __init__(self):
pass
def get(self, request):
context ={}
context['form']= MyForm()
return render(request, "home.html", context)
def post(self, request):
print(request.POST.get('the_choices'))
return HttpResponse('Great!')
模板-输入表单图像
控制台中的输出
请参考图2,我希望控制台中有1和2(首选组名),但它只返回2.