0

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:

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:
title = u'sdff'

Edit: Also, when I list through the items with a for loop, the title variable comes up.

2
  • 3
    Request Method: GET ... Request Method: GET Commented Aug 11, 2012 at 2:22
  • 1
    You are trying to access the POST dict in a GET request. The error is caused because you are doing a GET request to the page. Commented Aug 11, 2012 at 2:34

3 Answers 3

3

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. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also if you don't care whether it's in POST or GET you could do: data = getattr(request, request.method)
@D.A: Or you could just use request.REQUEST.
1

Are you sure that your request is really a 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.

Also, you should use if 'title' in request.POST: to check the presence of the variable in request...

Regards

Etienne

Comments

1

Firstly, this is a GET query, so the POST dict will be empty. Did you mean to set method='POST' in your form element in the HTML?

Secondly, KeyError means the key wasn't in the POST dictionary. 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.

1 Comment

I'm not sure what you mean "when I list through the items with a for loop, the title variable comes up." It would be useful to add what you actually got, because what you are saying shouldn't be possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.