【问题标题】:django form error :"the choice is not one of available choices"django 表单错误:“选择不是可用选择之一”
【发布时间】:2015-10-16 07:36:49
【问题描述】:

我有 2 个应用程序。

输入:具有包含下拉列表的表单:区域。下拉列表值来自结果数据库(由用户上传)。填写表格时,用户需要从下拉列表中选择值。

结果:有数据库

现在我可以在输入表单中显示“输入”应用程序数据库中的下拉列表。 (如图所示)。

我现在遇到的问题是选择并提交后,出现如下错误:

错误显示为:选择一个有效的选项。该选项不是可用选项之一。然后我不明白为什么,因为我确实从下拉列表中选择了选项。

提前感谢您帮助查明问题。

models.py

from django import forms
from django.forms import ModelForm
from django.db import models
from dupont.models import Result
from datetime import date
from django.forms import widgets

class Input(models.Model):
    company=models.CharField(max_length=100)
    region=models.CharField(max_length=100)

    def __unicode__(self):
        return self.company

forms.py

from django import forms
from django.forms import ModelForm
from .models import Input
from dupont.models import Result
from django.contrib.auth.models import User,Group
from django.forms import widgets
from functools import partial
from django.forms.utils import ErrorList

class InputForm(forms.ModelForm):
    company=forms.CharField(widget=forms.TextInput, label="Company",error_messages={'required': 'Please enter the company name'},required=True)
    region = forms.ModelChoiceField(queryset=Dupont.objects.values('region').distinct(),widget=forms.Select(),empty_label="(Global)",to_field_name="supply_chain")
    error_css_class='error'
    required_css_class = 'required'

    class Meta:
        model = Input
        fields = ('company', 'region')

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render,render_to_response,get_object_or_404
from inputform.forms import InputForm
from inputform.models import Input
from dupont.models import Result
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.list import ListView
from django.contrib import messages
from django.template import RequestContext
from django.shortcuts import redirect

@csrf_exempt
def input(request):
    if request.method == 'POST':
        form = InputForm(request.POST)
        if form.is_valid():
            company = form.cleaned_data['company']
            region = form.cleaned_data['region']    /Is this one correct?
            form.save()
            return redirect('result')
        else:
            print form.errors

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

html

<form method="post" action="{% url 'input' %}">
        {% csrf_token %}

        <!--company--> 
        <div class="field">
            {{ form.company.errors }}
            <label for="{{ form.company.id_for_label }}">Company:</label>
            {{ form.company }}
        </div>

        <!--Region-->
        <div class="field" >
            <label> Select the Region:
            {{ form.region }}
                {% for region in form.region.choices %}
                     <option value="region" name= "region" id="id_region">{{region}} </option>
                {% endfor %}
            </label>
        </div>

        <!--submit-->
        <div class="fieldWrapper">
        <p><input type="submit" value="Submit" /></p></div>

 </form>    

【问题讨论】:

    标签: python django


    【解决方案1】:

    感谢发帖:django forms give: Select a valid choice. That choice is not one of the available choices

    我换了

    iquery = Result.objects.values_list('region', flat=True).distinct()
    iquery_choices = [('', 'None')] + [(region,region) for region in iquery]
    region = forms.ChoiceField(iquery_choices,required=False, widget=forms.Select())
    

    替换之前的

    region=forms.ModelChoiceField(queryset=Dupont.objects.values('region').distinct(),widget=forms.Select())"
    

    然后它就可以工作了。但我不明白为什么,希望有人能解释一下。

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2018-04-30
      • 2012-05-13
      • 2016-05-18
      • 2011-11-02
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      相关资源
      最近更新 更多