By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

problems occured in Login function(Django)

P: 88
hi

i have small problems occurred in my login function, which i use Django to build,
in my template which is login.html, the code is like the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <title>Login</title>
  5. <link rel="stylesheet" type="text/css" href= "/HNV_css/hnv.css" >
  6. <script src="menuscript.js" type="text/javascript"></script>
  7. <script type="text/javascript">
  8. <!-- Begin
  9. function testLogIn(form) {
  10.     var validTxt = true;
  11.     if(document.memLogIn.username.value ==""){
  12.         validTxt = false;
  13.         alert("Please type in the user name");
  14.     }
  15.     else if (document.memLogIn.password.value == "") {
  16.         validTxt = false;
  17.         alert("Please type in the password")
  18.     }
  19.  
  20.     return validText;
  21.  
  22. }
  23.  
  24. // End -->
  25. </script>
  26. </head>
  27.  
  28. <body>
  29. <center><font face="Arial">
  30.         <h1>Login</h1></font></div>
  31.  
  32.  
  33.     {% if form.has_errors %}
  34.         <p>Sorry, that's not a valid username or password</p>
  35.     {% else %}
  36.  
  37.     {% endif %}
  38.  
  39.     <form action = "" method = "POST" name="memLogIn" onSubmit="return testLogIn(this)">
  40.  
  41.     <table border =0>
  42.     <tr><td><label for="id_username"> Name: </td><td> {{ form.username }} </td></tr>
  43.     <tr><td><label for="id_password"> Password: </td> <td> {{ form.password }} </td></tr>
  44.     </table>
  45.  
  46.     <input type="submit" value="login" />
  47.     <input type="hidden" name="next" value="{{ next }}" />
  48.  
  49.     </form>
  50.  
  51. </center>
  52.  
  53. </body>
  54. </html>
  55.  
in my views.py , the code is in the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def my_view(request):
  3.     username = request.POST['username']
  4.     password = request.POST['password']
  5.     user = authenticate(username=username, password=password)
  6.     if user is not None:
  7.         if user.is_active:
  8.             login(request, user)
  9.             return HttpResponseRedirect("/carbooking/")
  10.  
  11.         else:
  12.         return render_to_response('login_error.html')
  13.  
  14.     else:
  15.         # Return an 'invalid login' error message.
  16.          return render_to_response('login_error.html')
  17.  
i am not very sure where is wrong, but the error message always told me that

Request URL: http://localhost:8000/accounts/login/
Exception Type: MultiValueDictKeyError
Exception Value: "Key 'username' not found in <MultiValueDict: {}>"
Exception Location: c:\python25\lib\site-packages\django\utils\datastructures.py in __getitem__, line 136

i am quite new to Django framework, i did not really total understand what this error means, can anyone help me with this.. really thanks for any kind of help :)
Nov 5 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.