在使用django形式的ajax时,得到错误“选择一个有效的选择。这不是可用的选择之一。 [英] While using ajax with django form, getting error "Select a valid choice. That is not one of the available choices."

查看:169
本文介绍了在使用django形式的ajax时,得到错误“选择一个有效的选择。这不是可用的选择之一。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的新手。我使用简单的ajax在课程选择的基础上动态更新选择字段学期。但是,在提交表单时,我收到错误选择一个有效的选择。 选择的选项不是可用选项之一。代码如下:



forms.py:

从django导入表单
来自feedback_form.models import course,section_info

class loginForm(forms.Form) :
iquery1 = course.objects.values_list('course_name',flat = True)
iquery1_choices = [('','----------')] + [(id ,id)for id in iquery1]
sem_choices = [('','----------')]

course_name = forms.ChoiceField(iquery1_choices,required = True,widget = forms.Select())
semester = forms.ChoiceField(sem_choices,required = True,widget = forms.Select())

views.py:

  def get_batch(request,c_id) 
current_course = feedback_form.models.course.objects.get(course_name = c_id)
batches = feedback_form.models.batch.objects.all()。filter(course_id = current_course)
no_of_sem = feedback_form.models .course.objects.values_list('number_of_sem',flat = True).filter(course_id = current_course)
no_of_sem = int(no_of_sem [0])
batch_dict = {}
批次:
batch_dict [batch.batch_id] = batch.batch_id
sem = {}
sem [no_of_sem] = no_of_sem
data = [batch_dict,no_of_sem]
return HttpResponse(json.dumps(data))

loginForm.html:

 < form action =method =post> 
< table>
{{form.as_table}}
< / table>
{%csrf_token%}
< input type =submitvalue =Submit>
< / form>

< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / script>
< script>

$(document).ready(function(){
$('#id_course_name')。change(function(){
request_url ='login / get_batch /'+ c_id +'/';
$ .ajax({
url:request_url,
success:function(data){
data = $ .parseJSON(data);
$('#id_semester')。html('< option selected ='+selected+'>'+''+'< / option>');
= 1; i< = data [1]; i ++)// data [1]不包含sem
$('#id_semester')。append('< option value ='+ i +' >'+ i +'< / option>');
},
错误:function(e){
alert(e);
}
})

})

请帮助我。

解决方案

问题是,choiceField需要选择的选择才能在其选择集中。在上面的代码中, semester 是动态upda通过jquery,但这不是学期选择集的一部分,即 sem_choices 。所以问题出现了。要解决此问题,请选择 sem_choices 中的选定值。通过使用 request.POST 方法。



在views.py:

  form = loginForm(request.POST)
sem = request.POST.get('semester')
form.fields ['semester'] .choices = [(sem,sem)]


I am newbie in django. I am using simple ajax to dynamically update the choice field semester on the basis of course selection. But while submitting the form i am getting the error Select a valid choice. selected option is not one of the available choices. Code is as follow:

forms.py:

from django import forms
from feedback_form.models import course,section_info

class loginForm(forms.Form):
     iquery1 = course.objects.values_list('course_name', flat = True)
     iquery1_choices = [('', '----------')] + [(id, id) for id in iquery1]
     sem_choices = [('', '----------')]

     course_name = forms.ChoiceField(iquery1_choices,required=True, widget=forms.Select())
     semester = forms.ChoiceField(sem_choices, required= True, widget=forms.Select())

views.py:

def get_batch(request, c_id):
    current_course = feedback_form.models.course.objects.get(course_name=c_id)
    batches = feedback_form.models.batch.objects.all().filter(course_id=current_course)
    no_of_sem = feedback_form.models.course.objects.values_list('number_of_sem', flat=True).filter(course_id = current_course)
    no_of_sem = int(no_of_sem[0])
    batch_dict = {}
    for batch in batches:
         batch_dict[batch.batch_id] = batch.batch_id
    sem = {}
         sem[no_of_sem] = no_of_sem
    data = [batch_dict, no_of_sem]
    return HttpResponse(json.dumps(data))

loginForm.html:

 <form action="" method="post">
    <table>
        {{ form.as_table }}
    </table>
    {% csrf_token%}
    <input type="submit" value="Submit">
 </form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
$('#id_course_name').change(function() {
    request_url = 'login/get_batch/' + c_id + '/';
    $.ajax({
        url: request_url,
        success: function(data){
            data = $.parseJSON(data);
            $('#id_semester').html('<option selected="' + "selected" + '">' + '' +'</option>');
            for(var i = 1; i<=data[1]; i++) //data[1] contains no of sem
                $('#id_semester').append('<option value="' + i + '">' + i +'</option>');
        }, 
        errors: function(e) {
            alert(e);
        }
    })

})

Please help me out.

解决方案

The problem is that choiceField requires selected choice to be in its choice set. In above code the choices for semester is dynamically updating via jquery but this are not the part of semester's choice set i.e. sem_choices. Hence the problem is arising. To solve this problem include the selected value in sem_choices. By using request.POST method.

In views.py:

form = loginForm(request.POST)
sem = request.POST.get('semester')
form.fields['semester'].choices = [(sem, sem)]

这篇关于在使用django形式的ajax时,得到错误“选择一个有效的选择。这不是可用的选择之一。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆