需要一个简单的工作AJAX的例子Django的形式 [英] Need a simple working ajax example for django forms

查看:485
本文介绍了需要一个简单的工作AJAX的例子Django的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我需要类似的东西<一个href="http://www.w3schools.com/jquery/jquery_ajax_get_post.asp">http://www.w3schools.com/jquery/jquery_ajax_get_post.asp在Django完成。我已经下载了样品并在本地用本地主机+ PHP的测试,它的工作原理了罚款,但我似乎无法得到它在Django无论多么简单的例子就是工作。这基本上是根据从上面稍加修改链​​接

Basically I need something similar to http://www.w3schools.com/jquery/jquery_ajax_get_post.asp done in django. I've downloaded the samples and tested it locally with a localhost + php and it works out fine but I can't seem to get it to work in django no matter how simple the example is. Here's basically what I've done based on the example from the link above with slight modification

JavaScript的:

the javascript:

<script type="text/javascript">
$(document).ready(function(){
  $("#my_form").submit(function(){
    $.post("",
    {name:"Donald Duck",
     city:"Duckburg"},
    function(data,status){
      alert("Data: " + data + "\nStatus: " + status);
    })
    .fail(function() { alert("error"); });
    return false;
  });
});
</script>

网址:

url(r'^ajax/$', views.ajax_test, name="ajax"),

的意见:

def ajax_test(request):
    if request.method == 'POST' and request.is_ajax():
        name = request.POST['name']
        city = request.POST['city']
        message = name + ' lives in ' + city

        return HttpResponse(json.dumps({'message': message})) #tried without the json. Doesn't work either

    return render(request, 'books/ajaxTest.html')

在html:

the html:

<form id="my_form" action="" method="post" {% if form.is_multipart %}enctype="multipart/form-data"{% endif %}>{% csrf_token %}
<input type="submit" value="Send">
</form>

的形式是假设包括Django的形式,但因为我不能连得基础工作,这将是有点儿毫无意义。有人提到关于csrf_token标记,但删除不解决此问题有两个。上面的例子中的输出基本上只产生警报('错误'),而不是其他。我经历过这样的例子很多,但我甚至无法获得最基本的工作。

The form is suppose to include a django form but since I can't even get the basics to work, that would be kinda pointless. Someone mentioned about the csrf_token tag but removing that doesn't solve the problem either. The output of the above example basically just yields the alert('error') and nothing else. I've been through so many examples but I can't even get the most basic ones to work

推荐答案

OK..thx为您comments..I得到它全部整理out..basically我只是错过了{%csrf_token%}和csrfmiddlewaretoken上: {{csrf_token}}'..只是为那些谁可能是阅读this..the新的codeS会是这个样子的利益

OK..thx for your comments..I got it all sorted out..basically I just missed out on the {% csrf_token %} and csrfmiddlewaretoken:'{{ csrf_token }}'..just for the benefit of those who might be reading this..the new codes will look something like this

JavaScript的:

the javascript:

<script type="text/javascript">
$(document).ready(function(){

  $("#my_form").submit(function(){
    $.post("",
    {name:"Donald Duck",
     city:"Duckburg",
     csrfmiddlewaretoken:'{{ csrf_token }}'
     },
    function(data,status){
      alert("Data: " + data + "\nStatus: " + status);
    })
    .fail(function(xhr) {
        console.log("Error: " + xhr.statusText);
        alert("Error: " + xhr.statusText);
    });
    return false;
  });

});
</script>

在html:

the html:

<form id="my_form" action="" method="post">{% csrf_token %}
<input type="submit" value="Send">
</form>

这篇关于需要一个简单的工作AJAX的例子Django的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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