473,287 Members | 1,813 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,287 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 2365

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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.