在 Django 表单中使用 ajax 时,出现错误“选择一个有效的选择.那不是可用的选择之一." [英] While using ajax with django form, getting error "Select a valid choice. That is not one of the available choices."

查看:19
本文介绍了在 Django 表单中使用 ajax 时,出现错误“选择一个有效的选择.那不是可用的选择之一."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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))

登录表单.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);
        }
    })

})

请帮帮我.

推荐答案

问题在于 ChoiceField 要求所选选项位于其选择集中.

The problem is that ChoiceField requires the selected option to be in its choice set.

在上面的代码中,semester 的选择是通过 jquery 动态更新的.然而,这些选择不是 semester 选择集的一部分,即 sem_choices.因此问题来了.

In the above code, the choices for semester are dynamically updating via jquery. However, these choices are not the part of semester's choice set i.e. sem_choices. Hence the problem.

要解决此问题,请使用 request.POST 方法将选定的值包含在 sem_choices 中.

To solve this problem, include the selected value in sem_choices by using the request.POST method.

在views.py中:

In views.py:

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

这篇关于在 Django 表单中使用 ajax 时,出现错误“选择一个有效的选择.那不是可用的选择之一."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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