Is there a built-in login template in Django?

DjangoLoginTemplates

Django Problem Overview


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?

Django Solutions


Solution 1 - Django

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.

Solution 2 - Django

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.

For Django 2.1 and above, the function-based views have been removed in favour of class-based views:

from django.contrib.auth.views import LoginView

urlpatterns = [    ...    path('accounts/login/', LoginView.as_view(template_name='admin/login.html')),    ...]

Solution 3 - Django

The most upvoted response by @brant is technically incorrect. Django provides default views to handle login functionality but per the documentation does not provide a template:

>Django provides no default template for the authentication views. You should create your own templates for the views you want to use. The template context is documented in each view, see All authentication views.

Solution 4 - Django

Similar to mrts’ answer, in more recent Django, you can use the LoginView. You can further customize the template by setting template context like title, site_title etc. as used in admin/base.html so that it doesn’t look like an admin login.

from django.contrib.auth.views import LoginView

urlpatterns = [
    url(  
        r'^accounts/login/$',  
        LoginView.as_view(
            template_name='admin/login.html',
            extra_context={         
              'title': 'Login',
              'site_title': 'My Site',
              'site_header': 'My Site Login'},
        name='login'),
]

Solution 5 - Django

If you want to take a quick route to getting up and running I recommend using the URLConf provided.

for example:

urlpatterns = [
    url('^', include('django.contrib.auth.urls'))
]

See more details in the django documentation: https://docs.djangoproject.com/en/1.8/topics/auth/default/#module-django.contrib.auth.views

Solution 6 - Django

Quick and dirty.

Suppose, for example, you're working on a re-usable app that requires login functionality, but does not implement its own.

If, for the time being, you do not want to bother with custom login (views, forms, templates, urls, and so on), you can simply specify the admin login page as LOGIN_URL in settings.py (docs):

...

LOGIN_URL = '/admin/login/'

...

Beware: One serious drawback is that you can only log in with a staff account (or superuser).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionstanleyxu2005View Question on Stackoverflow
Solution 1 - DjangoBrantView Answer on Stackoverflow
Solution 2 - DjangomrtsView Answer on Stackoverflow
Solution 3 - DjangoJonny WafflesView Answer on Stackoverflow
Solution 4 - DjangoYushin WashioView Answer on Stackoverflow
Solution 5 - DjangoShaileshView Answer on Stackoverflow
Solution 6 - DjangodjvgView Answer on Stackoverflow