【问题标题】:Issue with my Django request from a JSON POST来自 JSON POST 的 Django 请求出现问题
【发布时间】:2014-03-22 20:38:49
【问题描述】:

我尝试访问一个 JSON 对象,从 JS-jquery 使用 $.post 发送到 Django 脚本,

我尝试了我在 Stackoverflow 上看到的许多东西的组合,但我无法让它发挥作用:

在 .js 方面:

$("#submitbtn").click(function() {
    var payload = {"name":"Richard","age":"19"};
    $("#console").html("Sending...");
    $.post("/central/python/mongo_brief_write.py",{'data': JSON.stringify(payload)},
                                           function(ret){alert(ret);});
    $("#console").html("Sent.");
});

我的脚本名为 mongo_brief_write.py 的内容是:

#!/usr/bin/env python
import pymongo
from pymongo import Connection
from django.utils import simplejson as json

def save_events_json(request):
    t = request.raw_post_data
    return t

con = Connection("mongodb://xxx.xxx.xxx.xxx/")
db = con.central
collection = db.brief
test = {"name":"robert","age":"18"}
post_id = collection.insert(t)

def index(req):
    s= "Done"
return s

如果我按下提交按钮,我会正确显示“完成”警报,但我的 mongoDB 中的集合中没有任何内容。

如果我用 test in 替换 t

post_id = collection.insert(test)

我也有完成警报,我的对象是在我的 mongo DB 集合中创建的。

我的错误在哪里?在我的 POST 请求中?我在 Apache 下工作,我使用 modpython。

【问题讨论】:

    标签: jquery python json django pymongo


    【解决方案1】:

    亲爱的@Kyrylo Perevozchikov,我已经更新了我的代码:

    import pymongo
    from pymongo import Connection
    from django.utils import simplejson as json
    from django.http import HttpResponse,HttpRequest
    request = HttpRequest()
    if request.method == 'POST':
        def index(request):
            global t
            t = request.raw_post_data                          
      post_id=Connection("mongodb://xxx.xxx.xxx.xxx/").central.brief.insert(t)
            return HttpResponse(content=t)
    else:
        def index(req):
            s="Not A POST Request"
            return s
    

    当我点击 jquery 按钮时,我有“不是 POST 请求”

    【讨论】:

      【解决方案2】:

      看起来它的发生是因为 python 命名空间规则。如果在函数中定义变量:

      >>>def random_func(input):
             t = input
             return t
      >>>t
      Traceback (most recent call last): File "<input>", line 1, in <module> 
      NameError: name 't' is not defined
      

      它不会是全局变量。 所以,你需要做的就是: 首先,将带有基本操作的代码放入函数 save_events_json:

      def save_events_json(request):
          t = request.raw_post_data
          con = Connection("mongodb://xxx.xxx.xxx.xxx/")
          db = con.central
          collection = db.brief
          test = {"name":"robert","age":"18"}
          post_id = collection.insert(t)
          from django.http import HttpResponse
          return HttpResponse(content=t)
      

      或将变量“t”设置为全局:

      def save_events_json(request):
          global t
          t = request.raw_post_data
          return t 
      

      【讨论】:

      • 亲爱的@Kyrylo Perevozchikov,感谢您的帮助,我理解您对全局变量的看法,这是有道理的,但不幸的是它并没有解决我的问题。我不明白的是,如果我删除这部分 .py 脚本:def index(req): s= "Done" return s,将所有行放在同一个 save_events_json 函数中,最后返回 t,我有 POST 500 内部服务器错误?
      • 亲爱的@Manuhoz,这是因为您返回的不是 HttpResponse 对象,请尝试“从 django.http 导入 HttpResponse”、“返回 HttpResponse(content=t)”
      • 我在下面发送了更多详细信息以正确格式化代码 thx @Kyrylo Perevozchikov
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 2018-09-02
      • 2019-04-28
      相关资源
      最近更新 更多