简体   繁体   English

在ajax调用中的Django 1.11 gae应用程序中request.POST为空

[英]request.POST is empty in django 1.11 gae app in ajax call

I updated my django google app engine application from 1.2 to 1.11 and do "revelant" steps (python 2.7), such as 我将django google app引擎应用程序从1.2更新到1.11,并执行“启示性”步骤(python 2.7),例如

urlpatterns = patterns('',     
   url(r'^$','froom.views.index', name='index'),  

to

urlpatterns =  [   
   url(r'^$',views.index, name='index'), 

and start to use django cripsy forms. 并开始使用django cripsy形式。

while I run 1.2 version and post form, and get request.POST has dict value with posted form values, 当我运行1.2版本并发布表单时,并获取request.POST具有字典值和发布的表单值,

however with 1.11 version, request.POST is empty. 但是,对于1.11版本,request.POST为空。

request.POST = <QueryDict: {}>

I double check my ajax calls comes with 我仔细检查我的Ajax通话是否附带

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

as noted here 作为注意到这里

My method stays same: 我的方法保持不变:

def edit_tenant(request, tenant_id=None):
    if (request.method == 'POST'):     
        if tenant_id:
            tenant_id_int = long(tenant_id)
            tenant_org = db.get(db.Key.from_path('Tenant', tenant_id_int))
            form = TenantForm(request.POST, instance = tenant_org, prefix = "edittenant")

django settings.py as below: django settings.py如下:

working version one's: 工作版本:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates'),
)

none working version one's: 没有可用的版本:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(os.path.dirname(__file__), 'templates'),
            os.path.join(os.path.dirname(__file__), 'myapplication'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

What could be issue that upgraded versions request.POST is empty? 升级版本request.POST为空可能是什么问题?

The issue resolved with "@Daniel Roseman" comments which points to csrf token: 该问题通过“ @Daniel Roseman”注释解决,该注释指向csrf令牌:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.auth', 
    'myapplication',
    'django_forms_bootstrap',
    'crispy_forms',     
)

when I add 'django.contrib.auth', 当我添加'django.contrib.auth',

and update : 并更新:

  return render_to_response('edit_tenant.html')

to

return render(request, 'edit_tenant.html') 返回render(request,'edit_tenant.html')

It puts 它把

 <input type='hidden' name='csrfmiddlewaretoken' value='itg3eY4YnesCy6p5enJS19txbMDI49Gf4UUEPQjOkUtyoEDBBO1L8nyborVD9prW' />

However I should have add 'django.middleware.csrf.CsrfViewMiddleware' to MIDDLEWARE_CLASSES as well: 但是我也应该在MIDDLEWARE_CLASSES添加'django.middleware.csrf.CsrfViewMiddleware'

MIDDLEWARE_CLASSES = (
    'django.middleware.csrf.CsrfViewMiddleware',
    'gaesessions.DjangoSessionMiddleware',
)

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

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