简体   繁体   English

Django给出错误“选择有效选择”。 Django 1.11.5版

[英]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.

相关问题 Django形式错误:RadioSelect()上的“选择有效选择” - Django form error: “Select a valid choice” on RadioSelect() Python Django - Select 选择无效 - Python Django - Select Choice is not Valid Django ChoiceField返回在基于类的视图中选择有效选择错误 - Django ChoiceField Returns Select a Valid Choice Error in Class Based View Django表单错误:选择一个有效的选项。 ......不是可用的选择之一 - Django Form Error: Select a valid choice. … is not one of the available choices “选择一个有效的选择。” Django中使用Modelform时出错 - “Select a valid choice.” Error using Modelform in Django Django ModelForm 中的错误。 选择一个有效的选项。 该选择不是有效选择之一 - Error in Django ModelForm. Select a valid choice. That choice is not one of the valid choices Django-错误-选择一个有效的选择。 [ <some choice> ]不是可用的选择之一 - Django - Error - Select a valid choice. [<some choice>] is not one of the available choices 选择一个有效的选项。 该选择不是可用的选择之一。 Django 表单出错 - Select a valid choice. That choice is not one of the available choices. error with Django Form 选择一个有效的选项。 该选择不是可用的选择之一——Django 表单 - Select a valid choice. That choice is not one of the available choices --Django forms Django:Select 是一个有效的选择。 该选择不是可用的选择之一 - Django: Select a valid choice. That choice is not one of the available choices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM