我可能错过了一些非常基本的东西,但是当我尝试通过以下方式访问帖子变量时: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: {}>"
该函数的完整代码如下:
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)
列出的后变量是这样的:
POST:
title = u'sdff'
编辑:另外,当我使用 for 循环列出项目时,会出现 title 变量。