473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

create Login function in Django

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
0 2371

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

Similar topics

28
by: flamesrock | last post by:
Firstly, this topic is NOT intended for trolling or starting any flame wars. I want to know if anyone has experience with these frameworks, and if so, how do they compare? Which one do you...
0
by: Sokolov Yura | last post by:
Django Model is wonderfull. But SQLObject more flexible (and powerfull, as i think, and has already more db interfaces). But Django Model is tied with Django, and using Django with another OO...
1
by: kernel1983 | last post by:
I look it up in PyPI There are info about Django: Package Score Description Django096 0.96 6 Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
4
by: seb.haase | last post by:
Hi! I was surprised when I did a google-groups search for python, ( http://groups.google.com/groups/search?q=python&qt_s=Search+Groups ) it shows these groups: comp.lang.python with about...
0
by: kang jia | last post by:
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: <html> <head>...
4
by: circularfunc | last post by:
i have been trying to get Django running for 2 days now and it drives me crazy. i played with webpy a bit and it is easy to get going with. but django seems like once you have it all up and...
4
by: K | last post by:
Hello everyone, I understand that urllib and urllib2 serve as really simple page request libraries. I was wondering if there is a library out there that can get the HTTP requests for a given...
1
by: Aspersieman | last post by:
On Wed, 05 Nov 2008 08:35:23 +0200, 3000 billg <billg3000@hos.twgg.org> wrote: Hi Excellent choice :)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.