[jQuery] Django flash my page automatically after a form submitting in jquery
Hi,
After struggling in google for days on this problem , I hope any of you could help me solve it.
My problem is that after I submit a form in jquery with $.post to the server, which is powered by Django. The post data has been sent to the server, and saved in the database, but the function of the the ajax success has not been executed because the page has been flashed.
At the same time, I found a log error in the server:
<blockquote> File "/usr/local/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 279, in run
self.finish_response()
File "/usr/local/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 318, in finish_response
self.write(data)
File "/usr/local/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 397, in write
self.send_headers()
File "/usr/local/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 461, in send_headers
self.send_preamble()
File "/usr/local/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 379, in send_preamble
'Date: %s\r\n' % http_date()
File "/usr/lib/python2.6/socket.py", line 297, in write
self.flush()
File "/usr/lib/python2.6/socket.py", line 284, in flush
self._sock.sendall(buffer)
error: [Errno 32] Broken pipe
</blockquote>So I thought it must be a Django problem; however, my data has been successfully saved to the database.
Here's the jquery code used:
<blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">$this.submit(function () {
var sentence=$(this).find('input:eq(0)').val();
var translation=$(this).find('input:eq(1)').val();
var keyword=key; //word is a global variable
$.post('addsentence/'+keyword, {
ensent:sentence,
cnsent:translation
},function () {
$('#sentadd').remove();
})
})
</blockquote><br clear="all">Here I'm going to post the two variable ensent and cnsent to the server.
And my corresponding view on Django is:
<blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">def add_sentence(request,word):
example=Example()
sentence=request.POST
word=Vocabulary.objects.get(word=word)
example.word=word
example.original=sentence["ensent"]
example.translation=sentence["cnsent"]
example.user=request.user
example.save()
return render('noResponse.xml')
</blockquote>
I thought they were very easy functions, but really out of my understanding what on earth the problem is! I really need your helps. Thanks in advance.
--
Samuel Wu