简体   繁体   English

Django:Django无法识别发布变量

[英]Django: Django not recognizing post variable

I'm probubly missing something very basic, but when I try to accsess a post varaible by means of: request['title'] in a function, I get this error: 我很可能缺少一些非常基本的东西,但是当我尝试通过函数中的request['title']来修饰一个帖子时,出现此错误:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/ajax/drafts/create

Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Knights',
 'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/Users/Mike/Desktop/Main/Django-Development/BBN/Knights/views.py" in document_create
  179.     title = request.POST['title']
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/utils/datastructures.py" in __getitem__
  258.             raise MultiValueDictKeyError("Key %r not found in %r" % (key, self))

Exception Type: MultiValueDictKeyError at /ajax/drafts/create
Exception Value: "Key 'title' not found in <QueryDict: {}>"

The full code for the function is this: 该函数的完整代码是这样的:

def document_create(request):
    user = request.user
    title = request.POST['title']
    if (title != ''):
        Draft.objects.create(content='Your content goes here', user=user, title=title)

and the post variables listed are this: 和列出的post变量是这样的:

POST:
title = u'sdff'

Edit: Also, when I list through the items with a for loop, the title variable comes up. 编辑:另外,当我通过for循环列出项目时,标题变量出现。

The request that caused the error was a GET, and you are trying to get the value of the title parameter from the POST dict. 导致错误的请求是GET,并且您正尝试从POST字典中获取title参数的值。 Change your code to: 将您的代码更改为:

def document_create(request):
    user = request.user
    title = request.GET['title']
    if (title != ''):
        Draft.objects.create(content='Your content goes here', user=user, title=title)

Or, you could test if the request is a GET or a POST, checking the request.method attribute. 或者,您可以通过检查request.method属性来测试请求是GET还是POST。

Are you sure that your request is really a POST? 您确定您的请求确实是POST吗? For me it look like your are trying to retrieve data from a POST request but the query is in reality a GET request. 对我来说,您似乎正在尝试从POST请求中检索数据,但查询实际上是GET请求。

Also, you should use if 'title' in request.POST: to check the presence of the variable in request... 另外,您应该if 'title' in request.POST:来检查request中变量的存在...

Regards 问候

Etienne 艾蒂安

Firstly, this is a GET query, so the POST dict will be empty. 首先,这是一个GET查询,因此POST字典将为空。 Did you mean to set method='POST' in your form element in the HTML? 您是否要在HTML的表单元素中设置method='POST'

Secondly, KeyError means the key wasn't in the POST dictionary. 其次, KeyError表示密钥不在POST词典中。 You should be doing: 您应该这样做:

title = request.POST.get('title', None)
if title is None:
    ...

This sets title to None if the key is not in request.POST . 如果密钥不在request.POST则将title设置为None

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

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