views:

83

answers:

1

Hello All Dear Developers,

Django is awesome. It has a built-in adminstration system. I have to write very less code to implemente user management module.

I want to let user sign in before seeing some page. Is there any built-in template for user sign-in, so that I do not have to write my own sign in page.

Best regards

+1  A: 

Yes. You can read all about it here: http://docs.djangoproject.com/en/1.2/topics/auth/#the-login-required-decorator ... but here are some bullet points:

  • add 'django.contrib.auth.middleware.AuthenticationMiddleware' to MIDDLEWARE_CLASSES in settings.py
  • add 'django.contrib.auth' and 'django.contrib.contenttypes' to INSTALLED_APPS in settings.py
  • setup a URL for the login using django.contrib.auth.views.login for the view, such as url(r'^login/$', 'django.contrib.auth.views.login',name="my_login")
  • In your view, include the login_required decorator and add it before your view. For example...

views.py...

from django.contrib.auth.decorators import login_required

@login_required
def home(request):
  return HttpResponse('Home Page')

By default, you then put the template inside my_template_directory/registration/login.html . Further info about that template can be found at the link in the beginning of this post.

Brant
Thanks for the information.I get an exceptioN: TemplateDoesNotExistHowever, the admin login page (also from template folder) exists.
stanleyxu2005
you can pass in a 'template_name':'path/to/your/template.html' argument in the urlconf to be sure that it's trying to read the template from the correct place
stevejalim
@stanleyxu2005 if you want to use the default template, you need to actually create it in a subfolder of your templates directory.
Brant
The django "logout" template does exist, only the "login" template cannot be found. I am stilling diving
stanleyxu2005
Thanks all of you. I have solved this problem by using the admin login/logout templates for normal users. # the built-in sign-in/out module (r'^accounts/logout/$', 'django.contrib.auth.views.logout'), (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}), (r'^accounts/$', 'django.views.generic.simple.redirect_to', {'url': '/'}), (r'^accounts/profile/$', 'django.views.generic.simple.redirect_to', {'url': '/'}),
stanleyxu2005