从django形式的AJAX获取POST数据 [英] Get POST data in django form AJAX from
问题描述
base.html:
< form method =POST> {%csrf_token%}
名字:< input type = 文本 >
< input type =submitvalue =Registerid =register>
< / form>
main.js:
$ b $($)$($)$($)$($)$($)$($) $ b $ .post(/,{
name:MY TEXT,
});
});
});
views.py:
$ b来自django.shortcuts的$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b def home(request):
if request.method =='POST':
print request.POST ['name']
return render_to_response('registration.html',{},
context_instance = RequestContext(request))
是的,我知道在这一刻我的JS不从文本形式获取真实数据,它只发送一个静态文本我的文本。但是当我按下按钮时,我得到
$ V $ $ $ $ $ $ $$$$$ p>我在做什么错?
我已经更改了我的代码:
main.js
($($)$($)$($)$($)$($)$($)$ e.preventDefault();
$ .post(/,{
name:'MY TEXT'
});
});
});
base.html
< form method =POST> {%csrf_token%}
名字:< input type =textname =name>
< br>
< input type =submitvalue =Registerid =register>
< / form>
它的作品,谢谢!
虽然我有两个问题:
- 在这种情况下,我在哪里发送我的文字?我发现它从名称字段返回ACTUAL数据,并且不返回我的文本
- 该页面仍在重新加载。而且我想让它完全成为AJAX。我的意思是创建一个python函数将数据从POST请求添加到MySQL数据库,并返回到AJAX脚本的操作结果。和一切没有页面重新加载。如何做到这一点?
发生了什么事情,你没有阻止浏览器默认提交
发生的动作。所以你的Ajax POST已经完成了,但是随后浏览器本身就会POST - 而且,如Michal所指出的那样,你的表单不包含一个名称
字段,所以查找失败。
您需要做两件事来解决这个问题。首先,在你的JS点击方法中使用 e.preventDefault();
。其次,请查看您视图顶部的 request.is_ajax()
。
I'm going to get parameter form AJAX request in Django, Here's what I'm doing:
base.html:
<form method="POST">{% csrf_token %}
First name: <input type="text">
<input type="submit" value="Register" id="register">
</form>
main.js:
$(document).ready(function(){
$("#register").live("click", function(e){
$.post("/", {
name: "MY TEXT",
});
});
});
views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
def home(request):
if request.method == 'POST':
print request.POST['name']
return render_to_response('registration.html', {},
context_instance=RequestContext(request))
Yes, I know that at this moment my JS doesnt get real data from text form, it sends just a static text "MY TEXT". but when I press button, I get "MultiValueDictKeyError at / "Key 'name' not found in ""
What I'm doing wrong?
I've changed my code: main.js
$(document).ready(function(){
$("#register").live("click", function(e){
e.preventDefault();
$.post("/", {
name:'MY TEXT'
});
});
});
base.html:
<form method="POST">{% csrf_token %}
First name: <input type="text" name="name">
<br>
<input type="submit" value="Register" id="register">
</form>
It works, thanks!
Although, I have two questions:
- Where am I sending 'MY TEXT' in this case? I ment it returns the ACTUAL data from name field and doesn't return "MY TEXT"
- The page is still reloading. And I wanted to make it completely AJAX. I mean create a python function to add data from POST request to MySQL database, and return to AJAX-script the result of the operation. And everything without page reload. How can I do this?
What's happening is that you haven't prevented the browser's default submit
action from taking place. So your Ajax POST is done, but then immediately the browser itself POSTs - and, as Michal points out, your form doesn't include a name
field, so the lookup fails.
You need to do two things to fix this. Firstly, use e.preventDefault();
in your JS click method. Secondly, check for request.is_ajax()
at the top of your view.
这篇关于从django形式的AJAX获取POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!