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?
- 347,512
- 102
- 1,199
- 985
- 8,081
- 14
- 59
- 94
-
3'Django provides **no default template** for the authentication views. You should create your own templates for the views you want to use.' – Maximiliano Padulo Oct 18 '17 at 13:48
6 Answers
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'toMIDDLEWARE_CLASSESinsettings.py - add
'django.contrib.auth' and'django.contrib.contenttypes'toINSTALLED_APPSinsettings.py - setup a URL for the login using
django.contrib.auth.views.loginfor the view, such asurl(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.
-
1Thanks for the information. I get an exceptioN: TemplateDoesNotExist However, the admin login page (also from template folder) exists. – stanleyxu2005 May 19 '10 at 16:28
-
1you 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 – Steve Jalim May 19 '10 at 16:47
-
@stanleyxu2005 if you want to use the default template, you need to actually create it in a subfolder of your templates directory. – Brant May 19 '10 at 16:54
-
The django "logout" template does exist, only the "login" template cannot be found. I am stilling diving – stanleyxu2005 May 20 '10 at 06:43
-
41Thanks 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 May 20 '10 at 07:19
-
3Would you mind to explain a little better what you have done to use the django admin templates on the login/logout views? – joaoricardo000 Jan 20 '14 at 12:19
-
In case anyone else found that admin suggestion useful, the correct redirect for urls.py in Django 1.5+ is: `url(r'^accounts/profile/$', RedirectView.as_view(url='/')), url(r'^accounts/$', RedirectView.as_view(url='/')),` – Julia Ebert Jul 09 '14 at 17:32
-
@JRicardo000: I did a bit of tinkering and summarised all these comments into the answer. Hope it helps. – Michael Scheper Dec 05 '14 at 05:57
-
My edit with the summary got rejected, so I'll submit it as two comments instead. Apologies for the formatting. To use the login template that's built into the admin site, you'll need these imports for a start: `from django.conf.urls import patterns, include, url` `from django.contrib import admin` `from django.views.generic.base import RedirectView` I'll put the guts in a separate comment. – Michael Scheper Dec 11 '14 at 07:09
-
`urlpatterns = patterns('',` ↵ ` # The admin app` ↵ ` url(r'^admin/', include(admin.site.urls)),` ↵ ` # As commented by @stanleyxu2005` ↵ ` url(r'^accounts/logout/$', 'django.contrib.auth.views.logout'),` ↵ ` url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),` ↵ ` # As commented by @Pterosaur` ↵ ` url(r'^accounts/profile/$', RedirectView.as_view(url='/')),` ↵ ` url(r'^accounts/$', RedirectView.as_view(url='/')),` ↵ ` # your own URLs go here` ↵ `)` – Michael Scheper Dec 11 '14 at 07:12
-
7"The template" - what template? That's precisely what the question is asking. There is no default template for authentication views according to the link you supplied and if you do not provide one, errors arise. – Alex Jun 08 '16 at 17:23
-
@alex, the suggestions above worked for me. This is my urls.py verbadim: – Rishi Jun 11 '16 at 06:12
-
from django.conf.urls import url from django.contrib import admin from django.conf.urls import patterns, include, url from django.contrib import admin from django.views.generic.base import RedirectView from django.contrib.auth.decorators import login_required urlpatterns = [ url(r'^admin/', admin.site.urls), – Rishi Jun 11 '16 at 06:12
-
url(r'^admin/', include(admin.site.urls)), url(r'^accounts/logout/$', 'django.contrib.auth.views.logout'), url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}), url(r'^accounts/profile/$', RedirectView.as_view(url='/')), url(r'^accounts/$', RedirectView.as_view(url='/')), ] – Rishi Jun 11 '16 at 06:12
-
-
I've found [this answer](https://stackoverflow.com/a/16630428/707650) to a duplicate question very useful: this provides all the extra goodies such as a password change form as well (all in Django admin style). – Jun 16 '17 at 10:03
-
1This is a good **yet incorrect** answer. The [documentation](https://docs.djangoproject.com/en/1.10/topics/auth/default/#module-django.contrib.auth.views) clearly states the short answer is **No**: "Django provides no default template for the authentication views. You should create your own templates for the views you want to use.". – Bruno Ciconelle Oct 26 '17 at 21:27
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')),
...
]
- 119
- 1
- 9
- 16,697
- 8
- 89
- 72
-
1Set `LOGIN_REDIRECT_URL = '/'` in your settings.py to redirect to a page other than /accounts/profile. – mehmet Nov 24 '15 at 00:06
-
3Support for string view arguments in `url()` [has been removed with Django 1.10](https://docs.djangoproject.com/en/1.11/releases/1.10/#features-removed-in-1-10). Use `from import django.contrib.auth.views import login` and `url(r'^accounts/login/$', login, ...` instead (and similar for logout). – Jun 16 '17 at 10:01
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.
- 16,697
- 8
- 89
- 72
- 720
- 5
- 9
-
Can anyone provide a working example of a login template? I have this one (https://gist.github.com/mellertson/05b36bc929fa4667c77010cf5932de0d) But, I'm not sure if it is correct. – MikeyE Jan 02 '19 at 06:19
-
1Murphie's law prevails again, because no sooner did I post my comment when I found a working example in the Django docs. I updated my gist code (above) so it now contains the example from the Django docs: https://docs.djangoproject.com/en/2.1/topics/auth/default/#module-django.contrib.auth.views – MikeyE Jan 02 '19 at 06:32
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'),
]
- 675
- 8
- 12
-
1For those who are looking for a sample template code, the `LoginView` mentioned in this answer does point to sample code for login.html – Anupam Mar 29 '21 at 08:58
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
- 595
- 3
- 17
-
And a sample login template on the same page; https://docs.djangoproject.com/en/1.8/topics/auth/default/#all-authentication-views There seems to be no built-in ones? And no registration either, looks like you need a third party module for that: https://www.djangopackages.com/grids/g/registration/ – Ciro Santilli OurBigBook.com May 08 '16 at 15:43
-
Self correction: some views do have built-in templates, like `accounts/password_reset/`, others don't like `accounts/login/`. – Ciro Santilli OurBigBook.com May 08 '16 at 16:01
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).
- 11,722
- 5
- 72
- 103