Reputation: 2403
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>
Upvotes: 0
Views: 3733
Reputation: 15808
I would highly recommend you use Django's built-in user authentication system. You can store additional user information for each user, and it will do most of the work for you (including encrypted passwords and user permissions).
If that is not possible, then look into using forms to collect and validate the user data - it is much cleaner and easier than doing it yourself.
Finally, I'd recommend you go over the tutorial to see how to use variables in your templates.
I know it takes a while to become familiar with how Django does things (god knows it took me long enough), but stick in there :).
Upvotes: 2
Reputation: 118488
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.
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",
{'invalid': True }) # our template can detect this variable
else:
return render_to_response("logedIn.html")
{% if invalid %}
Your email/password combo doesn't exist.
{% endif %}
Upvotes: 3