简体   繁体   English

Ajax帖子,但不调用Django View

[英]Ajax Posts but does not call Django View

The Problem 问题

After posting to my django view, the code seems to just stop halfway through. 发布到我的django视图后,该代码似乎只是停止了一半。

I have an ajax post to a Django view that is hard coded to render a specific response (see below). 我在Django视图中有一个ajax发布,该发布经过硬编码以呈现特定的响应(请参见下文)。 When I post to that view it should always return that response, but for some reason the view out puts all of the print statements, but not the query or the render. 当我发布到该视图时,它应该总是返回该响应,但是出于某种原因,该视图会放置所有的print语句,而不是查询或渲染器。 I know I'm being redirected by my sever log (shown below). 我知道我正在通过服务器日志重定向(如下所示)。

Any idea what could be causing this behavior? 知道是什么原因导致这种现象吗?

Server log: 服务器日志:

127.0.0.1 - - [26/Aug/2013 21:27:23] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/css/style.css HTTP/1.1" 304 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/js/map.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/js/home_jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/js/jquery_cookie/jquery.cookie.js HTTP/1.1" 304 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /favicon.ico HTTP/1.1" 404 -
I've been posted. Here are my values
<QueryDict: {u'name': [u'Mission Chinese Food'], u'address': [u'154 Orchard Street Manhattan']}>
Mission Chinese Food
154 Orchard Street Manhattan
Fish
127.0.0.1 - - [26/Aug/2013 21:27:32] "POST /results/ HTTP/1.1" 200 -

Simple Django View: 简单的Django视图:

def results(request):
    if request.method == 'POST':
        print "I've been posted. Here are my values"
        print request.POST
        print request.POST.get('name')
        print request.POST.get('address')
    restaurant = Restaurant.objects.filter(name='Fish', address='280 Bleecker St')[0]
    print restaurant
    return render(request, "stamped/restaurant.html", {'restaurant': restaurant}

Simple ajax post: 简单的ajax发布:

var send_data = { 'name': place.name, 'address': address};

    var csrftoken = $.cookie('csrftoken'); 

    alert(csrftoken);
    alert(send_data);

    function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    } 

    $.ajaxSetup({
        crossDomain: false, // obviates need for sameOrigin test
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type)) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    });

    $.ajax({ url: '/results/',
        type: 'POST',
        data: send_data,
        success: function(response) {
          console.log("everything worked!");
        },
        error: function(obj, status, err) { alert(err); console.log(err); }
      });




  });

Solved by using: 通过使用以下方法解决:

$.ajax({ url: '/results/',
    type: 'POST',
    data: send_data,
    success: function(response) {
      console.log("everything worked!");
      $("#results").html(response);
    },
    error: function(obj, status, err) { alert(err); console.log(err); }
  });

I was trying to use Django to redirect me to the render response like it ussually does. 我试图使用Django像往常一样将我重定向到渲染响应。 Because I'm calling from javascript to response is redriceted to javascript and the the typical django functions. 因为我是从javascript调用的,所以响应被重新定义为javascript和典型的django函数。

This part of the function 这部分功能

  success: function(response) {
          $("#results").html(response);
        }, 

renders the response to my chosen results div. 将响应呈现给我选择的结果div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM