使用Ajax和多个提交Django的表单验证 [英] Django form validation using Ajax and multiple submit

查看:392
本文介绍了使用Ajax和多个提交Django的表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经成功地实现使用dajax这个观点,但我觉得很混乱,因为我一直在阅读到目前为止,这是很好的做法,有你的观点,那么,在view.py文件。 我已经尝试实现它使用本指南: http://www.micahcarrick.com /ajax-form-submission-django.html 但没有能够读取单击该按钮。

So far I have succeeded in implementing this view using dajax, but I find it very messy and as I've been reading so far it is good practice to have your view, well, in the view.py file. I've tried implementing it using this guide: http://www.micahcarrick.com/ajax-form-submission-django.html but without being able to read which button is clicked.

同样的观点仍须提交(无重定向)的页面也包含绘图和我连接的仪器等的状态后显示出来。

The same view shall still be shown after submitting (no redirection) as the page also contains plotting and other statuses of my connected instruments.

models.py

models.py

class ActiveMeas(models.Model):
    channels    = models.CharField(max_length=100)
    technology  = models.ForeignKey(TechnologyModel)
    samples     = models.IntegerField()
    delay       = models.IntegerField()
    table       = models.IntegerField()
    stir        = models.IntegerField()

forms.py

forms.py

class ActiveForm(ModelForm):
    class Meta:
        model=ActiveMeas

HTML文件

html file

<form action="" method="post" id="activeform">
    <div id="ajaxwrapper">
    {% csrf_token %}
    {{ form.non_field_errors }}
    {{ form.as_p }}
    <p id="sendwrapper"><input type="submit" value="Start" id="idstartbut" name="_ButtonStart"/></p>
    <p id="sendwrapper"><input type="submit" value="Stop" id="idstopbut" name="_ButtonStop"/></p>
    </div>
</form>

和视图文件,我永远也进不了的 buttonstart buttonstop 的。另一个问题是,我怎么回数据的JavaScript,并从那里更新呢?

And the view file where I never get into the buttonstart or buttonstop. Another question is how do I return data to the javascript and update it from there?

views.py

def active(request):
    if request.POST:
        form = ActiveForm(request.POST)
        if '_ButtonStart' in request.POST:
            print "START"
            if form.is_valid():
                response_data = {'data':request.POST}
                return HttpResponse(simplejson.dumps(response_data))
            else:
                print "NOT VALID"
                response_data = {'data':request.POST}
                return HttpResponse(simplejson.dumps(response_data))
        elif '_ButtonStop' in request.POST:
            print "STOP"
    else:
        form = ActiveForm()
    return render(request, 'active.html', {'index': "active",'form':form})

的javascript

javascript

function ActiveInit(){
    $(document).ready(function () {
    $(function() {
        var form = $("#activeform");
        form.submit(function(e) {
            $("#ajaxwrapper").load(
                form.attr('action') + ' #ajaxwrapper',
                form.serializeArray(),
            );
            e.preventDefault(); 
        });
    });
}

我很新的这两个Django的,AJAX和jQuery。所以,请赐教,如果事情是必须要做的不同,以及如何解决我的问题。

I'm very new to both django, ajax and jQuery. So please enlighten me if something is to be done different, and how to solve my problem.

推荐答案

安静一些搜索后,我发现了jQuery插件的形式的 http://www.malsup.com/jquery/form/ 这确实exacly我想要的东西。

After quiet some more searching, I found the jQuery form plugin http://www.malsup.com/jquery/form/ which does exacly what I wanted.

所以,简单地我的JavaScript现在是:

So simply my javascript is now:

$(document).ready(function(){
    $('#aform').ajaxForm(function(data){
        if(data){
              //Do something with the returned data
        }
    });
});

这两者的形式发送,并提交值,以我的看法。

Which both sends the form and submit value to my view.

我的观点函数,那么转储错误的jQuery的再显示,如果任何

My view function then dumps the errors to jQuery for then to be shown if any

if request.POST:
    form = ActiveForm(request.POST)
    if form.is_valid():
        errors=""
        if '_ButtonStart' in request.POST:
            print "START"
        elif '_ButtonStop' in request.POST:
            print "STOP"
    else:
        print "NOT VALID stop"
        errors=form.errors
    return HttpResponse(simplejson.dumps(errors))
else:
    form = ActiveForm()
    return render(request, 'active.html', {'index': "active",'form':form})

在JavaScript中,错误就可以通过阅读:

In javascript, the errors can then be read by:

var errors = $.parseJSON(data);
console.log(errors.samples);     //Samples is an inputbox in my form 

这篇关于使用Ajax和多个提交Django的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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