create Login function in Django

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kang jia
    New Member
    • Jun 2007
    • 88

    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:

    [code=python]
    from django.contrib. auth import authenticate, login

    def my_view(request ):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(us ername=username , password=passwo rd)
    if user is not None:
    if user.is_active:
    login(request, user)
    # Redirect to a success page.
    return HttpResponseRed irect("/carbooking/")
    else:
    # Return a 'disabled account' error message
    return render_to_respo nse('login_erro r.html')
    else:
    # Return an 'invalid login' error message.
    return render_to_respo nse('login_erro r.html')
    [/code]

    but it got error message, it says that

    MultiValueDictK eyError at /accounts/login/
    "Key 'username' not found in <MultiValueDict : {}>"
    Request Method: GET
    Request URL: http://localhost:8000/accounts/login/
    Exception Type: MultiValueDictK eyError
    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 :
    [code=html]

    <html>
    <head>
    <title>Login</title>
    <link rel="stylesheet " type="text/css" href= "/HNV_css/hnv.css" >
    <script src="menuscript .js" type="text/javascript"></script>
    <script type="text/javascript">
    <!-- Begin
    function testLogIn(form) {
    var validTxt = true;
    if(document.mem LogIn.username. value ==""){
    validTxt = false;
    alert("Please type in the user name");
    }
    else if (document.memLo gIn.password.va lue == "") {
    validTxt = false;
    alert("Please type in the password")
    }

    return validText;

    }

    // End -->
    </script>
    </head>

    <body>
    <center><font face="Arial">
    <h1>Login</h1></font></div>


    {% if form.has_errors %}
    <p>Sorry, that's not a valid username or password</p>
    {% else %}



    {% endif %}




    <form action = "" method = "POST" name="memLogIn" onSubmit="retur n testLogIn(this) ">

    <table border =0>

    <tr><td> Name: </td><td> {{ form.username }} </td></tr>
    <tr><td> Password: </td> <td> {{ form.password }} </td></tr>
    </table>

    <input type="submit" value="login" />
    <input type="hidden" name="next" value="{{ next }}" />

    </form>

    </center>

    </body>
    </html>[/CODE]

    any one can help me with this? i really appreciate any kind of help :) Thanks.
    Last edited by bartonc; Oct 21 '07, 04:13 PM. Reason: tags are actually code=python and code=html
Working...