I have created a django app. It has got a user login/registration on the same registrationForm.html. The login functionality is working fine now. Once the user gives the correct username (here email id) and password the user is redirected to another page(logedIn.html) showing a message ‘User logged in’, and when the user is invalid (wrong username or password) , the page is again rendered to that page(registrationForm.html). Now i want to show a message on that page saying “Check your username or password” . Being a django newbie i am not able to do it. Can somebody help to do this. I will paste my code here.
views.py
def registrationForm(request):
if request.method == "POST":
firstName = request.POST.get("firstName")
lastName = request.POST.get("lastName")
email = request.POST.get("email")
password = request.POST.get("password")
sex = request.POST.get("sex")
birthday = request.POST.get("birthday")
print request.POST.get("sex")
UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()
return render_to_response('registrationForm.html')
return render_to_response("registrationForm.html")
def login(request):
if request.POST:
email=request.POST.get("username")
password = request.POST.get("password")
user = UniversityDetails.objects.filter(email=email,password=password)
if(not user):
return render_to_response("registrationForm.html")
else:
return render_to_response("logedIn.html")
html
<div align="center">
<form name="userInputForm" method="POST" id="myFormid" action="http://10.1.0.90:8080/login/">
<div style="float:left;width:100%;">
<p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Email id</label><br/> <input type="text" name="username" size="25" /></p>
<p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Password</label><br/><input type="password" name="password" size="25" /></p>
</div>
<p style="clear:both;float:left;"><input type="submit" value="Log in" /></p>
</div>
</form>
There are massive problems with the way you are doing things – storing plaintext passwords for example — look into the built in authentication backends available to django for how to properly log a user in. Added benefits include the user being available automatically in request.user
http://docs.djangoproject.com/en/dev/topics/auth/
BUT, I do understand learning django one step a time. Given your code, just add a variable to the context of your template being rendered on failure.
template