Django使用cleaning_data和ajax验证表单

时间:2015-05-17 10:18:54

标签: javascript jquery ajax django

我有一个严重的问题,我需要使用方法cleaning_data和方法getlist获取表单的值,因为我有ajax发送的数据。

但它不能正常工作,我的终端出现了这个错误:

Exception happened during processing of request from ('127.0.0.1', 56694)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 129, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
[17/May/2015 12:08:57] "POST /mafiche HTTP/1.1" 200 20921
error: [Errno 32] Broken pipe

我的form.html:

<form method="POST" id="formMod" action="{% url 'myfic' %}" >
    {% csrf_token %}
    {{ form.name|bootstrap }}
    {{ form.surname|bootstrap }}
    <div class="row">
            <div class="col-md-6">
                {{ form.comp_form|bootstrap }}
            </div>

            <div id="comp_details" class="col-md-6">
            </div>
    </div>
    ...
    <input type="submit" id="the_button" class="btn btn-success" value="Validate">

提交表单时,会调用此函数js:

function send_numbers(){
   $.post("http://localhost:8000/myfic",{"theNumbers[]": [1,2,3],});
}

$(document).ready(function() {
    $("#formMod").submit(function() {
        send_numbers();
    });
});

我的观点:

def myfic(request):
    if request.method == "POST" :
        form = AuthenticateForm(request.POST, request.FILES) #It's the form in the html
        numbers = request.POST.getlist("theNumbers[]")      

        if form.is_valid() :
            # Update
            print("OKKKKK")
        ...

如何获取我的表单和我的函数ajax的值?因为我摔倒了其中一个......

1 个答案:

答案 0 :(得分:0)

您需要return false;功能submit()以阻止默认浏览器提交。