[英]Django gives error “Select a Valid choice” . Django Version 1.11.5
My models.py is below: 我的models.py如下:
from django.db import models
from extensions.models import SipExtension
xmpp_users = SipExtension.objects.values_list('real_name',flat=True)
xmpp_users_choices = [('', 'None')] + [(real_name,real_name) for real_name in xmpp_users]
class xmpp_buddy_groups(models.Model):
group_name = models.CharField(max_length=30)
name = models.CharField(choices=xmpp_users_choices,max_length=100)
def __str__(self):
return '%s %s' % (self.group_name,self.name)
my forms.py is below: 我的forms.py如下:
from django import forms
from .models import xmpp_buddy_groups
from extensions.models import SipExtension
class xmpp_buddy_groups_form(forms.ModelForm):
xmpp_users = SipExtension.objects.values_list('real_name',flat=True)
xmpp_users_choices = [('', 'None')] + [(real_name,real_name) for real_name in xmpp_users]
name = forms.ChoiceField(xmpp_users_choices,required=True, widget=forms.SelectMultiple())
# name = forms.ModelChoiceField(widget=forms.CheckboxSelectMultiple,queryset=SipExtension.objects.values_list('real_name',flat=True),required=True)
class Meta:
model = xmpp_buddy_groups
fields = ['group_name']
my views.py is below: 我的views.py如下:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from .forms import xmpp_buddy_groups_form
@login_required
def index(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = xmpp_buddy_groups_form(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
#return HttpResponseRedirect('/index.html')
form.save()
# if a GET (or any other method) we'll create a blank form
else:
form = xmpp_buddy_groups_form()
print(form.errors)
return render(request, 'xmpp/index.html', {'form': form})
My template index.html is below 我的模板index.html在下面
{% extends 'base.html' %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create Buddy Groups">
</form>
{% endblock %}
The django gives me invalid choices error. django给了我无效的选择错误。 Image is attached.
图像已附加。
Thanks to help me. 谢谢帮助我。 It seems lime the choices for models and choices shown in templates through forms are not matching but I cannot figure out the reason since both are exactly same choices.
看来,模型的选择和通过表格显示在模板中的选择是不匹配的,但由于两者是完全相同的选择,我无法弄清原因。
models.py models.py
from django.db import models
from extensions.models import SipExtension
class xmpp_buddy_groups(models.Model):
group_name = models.CharField(max_length=30)
name = models.ForiegnKey(SipExtension)
def __str__(self):
return '%s %s' % (self.group_name,self.name)
forms.py 表格
from django import forms
from .models import xmpp_buddy_groups
from extensions.models import SipExtension
class xmpp_buddy_groups_form(forms.ModelForm):
class Meta:
model = xmpp_buddy_groups
fields = ['group_name']
def __init__(self, *args, **kwargs):
super(xmpp_buddy_groups_form, self).__init__(*args, **kwargs)
if self.instance:
self.fields['name'].initial = SipExtension.objects.get(pk=self.instance.SipExtension).real_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.