簡體   English   中英

在ajax調用中的Django 1.11 gae應用程序中request.POST為空

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

我將django google app引擎應用程序從1.2更新到1.11,並執行“啟示性”步驟(python 2.7),例如

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

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

並開始使用django cripsy形式。

當我運行1.2版本並發布表單時,並獲取request.POST具有字典值和發布的表單值,

但是,對於1.11版本,request.POST為空。

request.POST = <QueryDict: {}>

我仔細檢查我的Ajax通話是否附帶

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

作為注意到這里

我的方法保持不變:

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如下:

工作版本:

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

沒有可用的版本:

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',
            ],
        },
    },
]

升級版本request.POST為空可能是什么問題?

該問題通過“ @Daniel Roseman”注釋解決,該注釋指向csrf令牌:

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

當我添加'django.contrib.auth',

並更新:

  return render_to_response('edit_tenant.html')

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

它把

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

但是我也應該在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