Logging In
Part of the Users and Accounts section of Coddy's Django journey. Lesson 2 of 16.
The site can tell who is signed in, but it has no page to sign in on. Django has the view, LoginView. One line in the project's urls.py adds it, with the logout and password pages:
path("accounts/", include("django.contrib.auth.urls")),Now /accounts/login/ exists, named login. The view renders registration/login.html with a form that has a username and a password field. That template is yours to write: an ordinary POST form. Django looks for it in the templates/ folder of every app, and its name has no app in it, because Django's view does not know your apps.
After a good login the view redirects to LOGIN_REDIRECT_URL. Its default, /accounts/profile/, does not exist here, so set it in settings.py.
Challenge
Easy- In
mysite/urls.py, includedjango.contrib.auth.urlsunderaccounts/. - Create
notes/templates/registration/login.html: it extendsnotes/base.htmland shows theformin a POST form with aLog inbutton. - A good login lands on
/. - Under
You are not signed in., a linkLog in.
Press Run, then log in as ada (password coddy-pass-123).
Try it yourself
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("notes.urls")),
]
All lessons in Users and Accounts
Practice on your own: Django playground