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:
-
from django.contrib.auth import authenticate, login
-
-
def my_view(request):
-
username = request.POST['username']
-
password = request.POST['password']
-
user = authenticate(username=username, password=password)
-
if user is not None:
-
if user.is_active:
-
login(request, user)
-
# Redirect to a success page.
-
return HttpResponseRedirect("/carbooking/")
-
else:
-
# Return a 'disabled account' error message
-
return render_to_response('login_error.html')
-
else:
-
# Return an 'invalid login' error message.
-
return render_to_response('login_error.html')
-
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 :
-
-
<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.memLogIn.username.value ==""){
-
validTxt = false;
-
alert("Please type in the user name");
-
}
-
else if (document.memLogIn.password.value == "") {
-
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="return 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>
any one can help me with this? i really appreciate any kind of help :) Thanks.