MultiValueDictKeyError when accessing POST items

3,337 views
Skip to first unread message

DragonSlayre

unread,
Dec 22, 2008, 10:03:15 PM12/22/08
to Django users
I've created my own form:

<form method="POST" action="/accounts/login/">
<label id="login" style="font-weight:bold;">Login:</label>
<label id="login_username" value="{{ username }}">Username: </label>
<input type="text" />
<label id="login_password">Password:</label>
<input type="password" />
<input type="submit" value="login" />
</form>

I am mappying /accounts/login/ to a view of mine, which is then
calling:

if request.method == 'POST':
username = request.POST['login_username']

I have no idea why I'm getting the error - "Key 'login_username' not
found in <QueryDict: {}>".

What am I doing wrong?

Malcolm Tredinnick

unread,
Dec 22, 2008, 10:23:55 PM12/22/08
to django...@googlegroups.com

login_username is the value of the id attribute on the label, not on the
form input element. Basically, your HTML isn't correct for the type of
form you're after. You need to identify the form input elements, rather
than the label elements. Compare the output from a Django form to what
you have an you should see the difference fairly quickly.

Regards,
Malcolm


DragonSlayre

unread,
Dec 23, 2008, 12:27:15 PM12/23/08
to Django users
Cheers :)

Damn, I must have been a bit tired coding that up lol

Here's my working code (incase anyone else is as stupid as me):

<form method="POST" action="/accounts/login/">
<label style="font-weight:bold;">Login:</label>
<label for="login_username">Username: </label>
<input id="id_login_username" name="login_username" type="text"
value="{{ username }}" />
<label for="login_password">Password:</label>
<input id="id_login_password" name="login_password" type="password" /
>
<input type="submit" value="login" />
</form>

On Dec 23, 4:23 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

esatte...@wi.rr.com

unread,
Jan 21, 2009, 2:52:53 PM1/21/09
to Django users
HTML
<form id="login_form" name="login_form" accept-charset="utf-8"
method="post" action="">
<fieldset>
<img style="left: 20px; position: relative; top: 10px; margin-top:
-22px; padding-top: 5px;" src="http://media.muskegohitmen.com/images/
siteLogo_sm.png"/>
<li class="bold">Username: <br/><input type="text" size="40"
name="username" maxlength="30" id="id_username"/></li>
<li class="bold">Password: <br/><input type="password" size="40"
name="password" maxlength="20" id="id_password"/></li>
<li><a class="js-login btn_sil">Login</a><a class="btn_yellow">Forgot
Password ?!</a></li><input type="submit" value="Login" name="login"/>
</fieldset>
</form>

# JS - there is a little mootools in there, but though that might be
the problem so I used the document. to see if that would do it
function hitmenFormSubmit(form_id)
{
form = form_id.get('id');
this.alert("form submit " +" "+form);
document.login_form.submit();
//$(form).submit();
}

The HTML button is on the page for debugging. The HTML button works
just fine. but the javascript button results in this error.

Views function is looking for request.POST ['login'] == Login

Why would a javascript submit not post the same data?

On Dec 22 2008, 9:23 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

esatte...@wi.rr.com

unread,
Jan 21, 2009, 2:49:05 PM1/21/09
to Django users
I've run into a similar problem, but am trying to submit the form with
a text link and java script.

The submit element is on the page, but hidden. and I get the same
error:

---------------------------- HTML
------------------------------------------
<form id="login_form" name="login_form" accept-charset="utf-8"
method="post" action="">
<fieldset>
<img style="left: 20px; position: relative; top: 10px; margin-top:
-22px; padding-top: 5px;" src="http://media.muskegohitmen.com images/
siteLogo_sm.png"/>
<li class="bold">Username: <br/><input type="text" size="40"
name="username" maxlength="30" id="id_username"/></li>
<li class="bold">Password: <br/><input type="password" size="40"
name="password" maxlength="20" id="id_password"/></li>
<li><a class="js-login btn_sil">Login</a><a
class="btn_yellow">Forgot Password ?!</a>
</li><input type="submit" value="Login" name="login"/>
</fieldset>
</form>
-------------------------- EXTERNAL JS FUNCTION
---------------------------------------

// There is some mootools code in there, I thought that might be the
problem, so I stuck the document line in there to see if that would
fix it.
function hitmenFormSubmit(form_id)
{
form = form_id.get('id');
this.alert("form submit " +" "+form);
document.login_form.submit();
//$(form).submit();
}

my views function is looking for request.POST['login'] == Login

Any ideas?

If i press input button, it works fine, if i press the javascript
button I get the error
On Dec 22 2008, 9:23 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Jan 21, 2009, 5:23:51 PM1/21/09
to django...@googlegroups.com
On Mon, 2008-12-22 at 19:03 -0800, DragonSlayre wrote:

There is no form field element in your HTML called login_username, just
as the error message suggests. You have a label element with an id of
login_username, but that isn't a form field.

Malcolm


workingbird

unread,
Jan 22, 2009, 4:53:36 AM1/22/09
to Django users
"login_username" should be the attribute named "name", not "id".
Reply all
Reply to author
Forward
0 new messages