Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in login template in Django?

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

like image 675
stanleyxu2005 Avatar asked May 19 '10 15:05

stanleyxu2005


People also ask

How do I login as user in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...

Where can I find templates in Django?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.


2 Answers

As mentioned in the comments by the author, the easiest way to do this is to add the following lines to urls.py:

from django.contrib.auth.views import login, logout  urlpatterns = patterns('',     url(r'^accounts/login/$', login, {'template_name': 'admin/login.html'}),     url(r'^accounts/logout/$', logout), ) 

As far as I know, adding the r'^accounts/$' and r'^accounts/profile/$' URLs is not necessary unless user profile management is required.

As suggested by @mmatt in comments, set LOGIN_REDIRECT_URL = '/' in settings.py to avoid the default redirect to /accounts/profile/ after login. See LOGIN_REDIRECT_URL in Django settings documentation.

This should also still work in Django 2.x using path instead of url appropriately.

like image 33
mrts Avatar answered Sep 20 '22 02:09

mrts


Yes. You can read all about it here: https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.decorators.login_required ... 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.

like image 164
Brant Avatar answered Sep 24 '22 02:09

Brant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!