【问题标题】:django ajax requestdjango ajax 请求
【发布时间】:2013-02-15 08:17:04
【问题描述】:

我在使用 Ajax 发送表单时遇到问题。我有一个表单,我想在单击下一步按钮时异步替换为另一个表单。

这是脚本

$(document).ready(function() {
    $('#MY_FORM').submit(function() { 
        $.ajax({
            data: $(this).serialize(), 
            type: $(this).attr('method'),
            url: $(this).attr('action'), 
            success: function(response) { 
                $('#FORM_DIV').html(response); 
            }
        });
        return false;
    });
});

form.py

class CountyForm(forms.Form):
    county = forms.ModelChoiceField(queryset=County.objects.all(),
              empty_label='---Select a county---', required=False)
    other = forms.CharField(required=False)

    def __init__(self, *args, **kwargs):
        super(CountyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.html5_required = True
        self.helper.form_id = 'MY_FORM'
        self.helper.add_input(Submit('next', 'Next', css_class='classfinish'))
        self.helper.layout = Layout('county','other')


class WardForm(forms.Form):
    ward = forms.ModelChoiceField(queryset=Ward.objects.all(),
             empty_label='Select a ward')
    other = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(WardForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.html5_required = True
        self.helper.add_input(Submit('save', 'Finish'))
        self.helper.add_input(Submit('cancel', 'Cancel'))
        self.helper.layout = Layout('ward','other')

观看次数

def location(request):
    if request.is_ajax() :
        wardform = WardForm()
        return HttpResponse(wardform)
    countyform = CountyForm()
    c = {}
    c.update(csrf(request))
    return render(request,'location.html', {'countyform': countyform})

我希望当我点击县表单中的下一步按钮时,会出现病房表单。

【问题讨论】:

  • 你有什么问题?不过,您在 #FORM_DIV 之后缺少报价。
  • 你的JS有语法错误
  • @Pavel 打错了,谢谢。
  • 在您使用的任何浏览器中打开开发人员工具,您应该能够掌握 javascript 错误。以下是有关如何在不同浏览器中查找开发人员工具的详细信息 - computerworld.com/s/article/9225073/…
  • 这看起来好像您想升级到表单向导而不是两个表单部分类型的解决方案。 :)

标签: jquery python ajax django


【解决方案1】:

这可以通过两种方式解决,我不会介绍 FormWizard,但我会给你一个非常好的文档链接。

我的建议是您应该使用 FormWizard(取决于您拥有的 Django 版本),但我认为它已在 1.2 及更高版本中迁移到 Django,但请不要引用我的话。

在你目前的困境中,你应该按照这个思路做点什么。

$(document).ready(function() {
    $('#MY_FORM').submit(function() { 
        $.ajax({
            data: $(this).serialize(), 
            type: $(this).attr('method'),
            url: $(this).attr('action'), 
            success: function(response) { 
                $('#FORM_DIV').load('/some/url/to/a/wardform'); //new code 
            }
        });
        return false;
    });
});

views.py

def ward_form_renderer(request):
    if request.is_ajax():
        ward_form = WardForm()
        #depending on version of django
        context = {'wardform': ward_form}
        context.update(csrf(request))
        render(request, 'wardform_template', c)

这会做的是它会拉出一个带有渲染表单的新 html 页面 从您的 Django 视图中并显示它。基本上你所做的是一个FormWizard。 这种方法会有其他副作用,所以我不建议这样做。

我自己也参与了一个这样做的项目,说实话,痛苦多于快乐。 我自己没有尝试过这段代码,但你知道它应该如何工作的要点。

输入表单向导!这对您来说是一个特别好的选择,因为您不必重新定义表单,您可以使用已有的表单。

Here's the link to the FormWizard docs

祝你好运!

【讨论】:

    猜你喜欢
    • 2014-04-28
    • 2020-10-30
    • 2019-02-25
    • 2020-06-17
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多