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

create Login function in Django

P: 88
hi

i am currently learning Django framework and i have to do a login page at the moment. i have used the code like following:

Expand|Select|Wrap|Line Numbers
  1. from django.contrib.auth import authenticate, login
  2.  
  3. def my_view(request):
  4.     username = request.POST['username']
  5.     password = request.POST['password']
  6.     user = authenticate(username=username, password=password)
  7.     if user is not None:
  8.         if user.is_active:
  9.             login(request, user)
  10.             # Redirect to a success page.
  11.              return HttpResponseRedirect("/carbooking/")
  12.         else:
  13.             # Return a 'disabled account' error message
  14.             return render_to_response('login_error.html')
  15.     else:
  16.         # Return an 'invalid login' error message.
  17.         return render_to_response('login_error.html')
  18.  
but it got error message, it says that

MultiValueDictKeyError at /accounts/login/
"Key 'username' not found in <MultiValueDict: {}>"
Request Method: GET
Request URL: http://localhost:8000/accounts/login/
Exception Type: MultiValueDictKeyError
Exception Value: "Key 'username' not found in <MultiValueDict: {}>"

i did not really understand this error message, what should i do so that i can successfully create login page.

my login.html is like this :
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.  
  38.  
  39.     {% endif %}
  40.  
  41.  
  42.  
  43.  
  44.     <form action = "" method = "POST" name="memLogIn" onSubmit="return testLogIn(this)">
  45.  
  46.     <table border =0>
  47.  
  48.     <tr><td> Name: </td><td> {{ form.username }} </td></tr>
  49.     <tr><td> Password: </td> <td> {{ form.password }} </td></tr>
  50.     </table>
  51.  
  52.     <input type="submit" value="login" />
  53.     <input type="hidden" name="next" value="{{ next }}" />
  54.  
  55.     </form>
  56.  
  57. </center>
  58.  
  59. </body>
  60. </html>
any one can help me with this? i really appreciate any kind of help :) Thanks.
Oct 21 '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.