How to create django custom login page?

8,796 views
Skip to first unread message

Nge Nge

unread,
Apr 1, 2011, 3:03:24 AM4/1/11
to Django users
Hi All

I saved the registered user data into my postgresql database.
Now I have to create login page. I have to check the requested user
name and password are in my database. If user name and password are in
my database, redirect to success.html. How should I do? Pls advise
me.

Thanks
nge

gladys

unread,
Apr 1, 2011, 11:45:48 AM4/1/11
to Django users
I assume you are saving your user data into a
django.contrib.auth.models.User object?

I recommend using django's authentication framework,
http://docs.djangoproject.com/en/dev/topics/auth/ with all its
predefined views for logging in, etc, you will just need to provide
the html pages.

Now, if you want a custom login page, you will need the following:
* login url in urls.py
* login view for handling authentication

And your view can have something like this:
def user_login(request):
try:
# you might want to get user and pass from an html POST
# authenticate here using django.contrib's authentication
functions
from django.contrib.auth import authenticate, login
user = authenticate(username=user,password=pass)
if user is not None:
login(request,user)
# redirect to success.html
except MyUser.DoesNotExist:
pass
# render the login page

For more info, http://docs.djangoproject.com/en/dev/topics/auth/#authentication-in-web-requests

Vjacheslav

unread,
Apr 2, 2011, 6:27:18 PM4/2/11
to Django users
I suggest you to start from learning django.contrib.auth app sources.
Using them as your starting point should save a lot of your time:

1. Authorization form:
http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/forms.py#L61

2. Login view:
http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py

3. And html template
http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templates/admin/login.html#L30

Vjacheslav

unread,
Apr 2, 2011, 6:30:45 PM4/2/11
to Django users

Andre Terra

unread,
Apr 2, 2011, 8:35:54 PM4/2/11
to django...@googlegroups.com, Vjacheslav
For the sake of clarity and future reference, let me suggest another approach that will suffice for some use cases.

Sometimes your need can be solved simply by extending upon the original login page, without rewriting anything. For example, if you would like to take e-mails rather than usernames (or even both), all you will need is a custom template (saying "e-mail" where it currently reads "username") and a custom backend which will try to match user accounts to the provided information.


Example code that will authenticate an user against either e-mails or usernames:



Cheers,
André Terra (airstrike)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages