Django的帖子不工作: [英] Django Posts Not Working:

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

问题描述

我使用Django 1.2.3开发一个网站。我的AJAX GET请求做工精细,但POST请求在开发模式(127.0.0.1:8000)工作,但不是我推的网站投入生产使用Apache + nginx的。

I am using Django 1.2.3 to develop a site. My ajax get requests work fine but the post requests work in development mode (127.0.0.1:8000) but not when I push the site into production using apache + nginx.

下面是一个例子

urls.py:

(r'api/newdoc/$', 'mysite.documents.views.newdoc'),

views.py

views.py

def newdoc(request):
    # only process POST request
    if request.is_ajax():
        data= dict(request.POST)

                # save data to db



    return HttpResponse(simplejson.dumps([True]))

在javascript:

in javascript:

$.post("/api/newdoc/", {data : mydata}, function(data) { alert(data);}, "json");

我的提醒是从来没有所谓的....这是一个问题,因为我想通过一个Django的形式来净化这个数据和岗位要求似乎并没有使它的服务器(在生产中只)做的。

my alert is never called .... this is a problem because i want to sanitize this data via a django form and the post requests do not seem to making it to the server (in production only).

我在做什么错了?

更新:

解决方案:CRSF令牌需要被推动AJAX POST请求(不得到)作为Django的1.3

solution: crsf tokens need to be pushed ajax post requests (not gets) as of django 1.3

此外,每个链路提供以下,下面的JavaScript

also, per the link provide below, the following javascript

$.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                // Only send the token to relative URLs i.e. locally.
                xhr.setRequestHeader("X-CSRFToken",
                                     $("#csrfmiddlewaretoken").val());
            }
        }
    });

需要改变如下:

needs to be changed as follows:

$.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                // Only send the token to relative URLs i.e. locally.
                xhr.setRequestHeader("X-CSRFToken",
                                     $('input[name="csrfmiddlewaretoken"]').val());
            }
        }
    });

在CSRF令牌的形式被呈现方式必须有1.25的变化 - 1.3? 无论如何,它的工作原理。感谢您的帮助大家

the way the csrf token gets rendered in the form must have changed between 1.25 - 1.3?? regardless, it works. thanks for all your help everyone

推荐答案

你能直接从生产服务器访问您的JavaScript文件?其中Django的你在生产中使用的版本?如果您使用的是1.2.5+在生产中,您将需要一个AJAX后期操作过程中推动CSRF令牌到服务器。

Can you directly access your javascript files from the production server? Which Django version are you using in production? If you are using 1.2.5+ in production, you will need to push the csrf token to the server during an AJAX post operation.

请参阅在1.2.5版本说明和<一HREF =htt​​p://docs.djangoproject.com/en/1.2/ref/contrib/csrf/#ajax相对=nofollow> CSRF

要检查你的Django版本:

To check your Django version:

import django
django.get_version()

打印上面在生产站点或从壳生产服务器,同时确保你使用了正确的Python的路径。

Print the above in your production site or from the shell in your production server while making sure you are using the proper Python path.

这篇关于Django的帖子不工作:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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