Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在django中动态更改表单_Jquery_Django_Django Forms - Fatal编程技术网

Jquery 在django中动态更改表单

Jquery 在django中动态更改表单,jquery,django,django-forms,Jquery,Django,Django Forms,有没有办法通过ajax加载django表单?假设用户需要根据需要更改表单。首先出现一个带有choicefield和decimalfield的窗体。然后,根据choicefield的值,对视图的ajax请求可以将其更改为另一种形式或保持不变: forms.py a_choices = ( ("a", "A"), ("b", "B"), ("c", "C"), ) d_choices = ( ("d", "D"), ("e", "E"), ) class simpleForm(forms.Fo

有没有办法通过ajax加载django表单?假设用户需要根据需要更改表单。首先出现一个带有choicefield和decimalfield的窗体。然后,根据choicefield的值,对视图的ajax请求可以将其更改为另一种形式或保持不变:

forms.py

a_choices = (
("a", "A"),
("b", "B"),
("c", "C"),
)

d_choices = (
("d", "D"),
("e", "E"),
)


class simpleForm(forms.Form):
    #this is the first form
    def __init__(self, *args, **kwargs):
        choices = kwargs.pop('method')
        super(simpleForm, self).__init__(*args, **kwargs)
        self.fields["chosen_method"].choices = choices

    chosen_method = forms.ChoiceField(label="Método")
    simple_variable = forms.DecimalField()

class complexForm(simpleForm):

    second_variables = forms.DecimalField()
    third_variable = forms.DecimalField()
我尝试了这样一种ajax方式,谁在观察choicefield值(#id_selected_method)的变化:

ajax_form.js

(function ($) {
explanation = function () {
    $("#id_chosen_method").change(function () {
        var election = $("#id_chosen_method").val();
        // Add or remove fields depending of method chosen
        $.getJSON("/form2/" + election + "/", function (data) {
            if (data)
            {
                $("#form_fields").html(data.form);
                $("#explanation_text").html(data.explanation);
            }
            else
            {
                $("#form_fields").html("no form!");
                $("#explanation_text").html("no explanation!");
            }
        });
    });
};
})(jQuery);
最后是获取javascript函数传递的“method”参数的url和视图:

#url.py
url(r'^/form2/(?P<method>\w+)/$', ajax_form, name='ajax_form'),

#views.py
def ajax_form(request, method):

    import json
    from app.forms import simpleForm, complexForm
    from app.otherFile import explanations

    if request.is_ajax:
        form_choices = (("a", "b", "c",),("f", "g"))
        if method in form_choices[0]:
            if method == form_choices[0][-1]:
                form = simpleForm(method=a_choices)
            else:
                form = simpleForm(method=d_choices)
        else:
            if method == form_choices[1][1]:
                form = complexForm(method=a_choices)
            else:
                form = complexForm(method=d_choices)

        explanation = explanations[method]
        data = {"form": form, "explanation": explanation}
        return HttpResponse(json.dumps(data), mimetype="application/javascript")
    else:
        raise Http404
#url.py
url(r'^/form2/(?P\w+/$),ajax\u form,name='ajax\u form'),
#views.py
def ajax_表格(请求、方法):
导入json
从app.forms导入simpleForm、complexForm
从app.otherFile导入说明
如果request.is_ajax:
表格(a,b,c,),(f,g)
形式_选项[0]中的if方法:
如果方法==形式_选项[0][1]:
形式=简单形式(方法=a_选项)
其他:
形式=简单形式(方法=d_选项)
其他:
如果方法==表格[1][1]:
形式=复杂形式(方法=选择)
其他:
形式=复杂形式(方法=d_选项)
解释=解释[方法]
数据={“形式”:形式,“解释”:解释}
返回HttpResponse(json.dumps(data),mimetype=“application/javascript”)
其他:
提高Http404

因此,最终的想法是用户根据首先显示的choicefield的val选择所需的表单。但我不能让它工作。我错过什么了吗?有更好的方法来处理这样的事情吗?

我会使用一个基表单类,并从基表单继承两个不同的表单。根据用户输入,您可以通过ajax加载不同的表单。您甚至可以对表单使用相同的模板。你的观点应该是

form=simpleFormA()

基于什么是“方法”。forms.py将更改为以下内容:

_a_choices = (
("a", "A"),
("b", "B"),
("c", "C"),
)

_d_choices = (
("d", "D"),
("e", "E"),
)


class simpleForm(forms.Form):
    # this is the first form
    # all other fields here
    simple_variable = forms.DecimalField()

class simpleFormA(simpleForm):
    chosen_method = forms.ChoiceField(label="Método", choices=_a_choices)

class simpleFormB(simpleForm):
    chosen_method = forms.ChoiceField(label="Método", choices=_b_choices)

有意义吗?

当我调用simpleFormA时,它在呈现时引发了一个
捕获的名称错误:名称“self”未在forms.py行中定义
self.fields[“selected\u method”]。choices=\u choices
此线程帮助我解决了过去一天我试图自己解决的一个问题。谢谢你们!
_a_choices = (
("a", "A"),
("b", "B"),
("c", "C"),
)

_d_choices = (
("d", "D"),
("e", "E"),
)


class simpleForm(forms.Form):
    # this is the first form
    # all other fields here
    simple_variable = forms.DecimalField()

class simpleFormA(simpleForm):
    chosen_method = forms.ChoiceField(label="Método", choices=_a_choices)

class simpleFormB(simpleForm):
    chosen_method = forms.ChoiceField(label="Método", choices=_b_choices)