Menu
CoddyTech

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 icon

Challenge

Easy
  • In mysite/urls.py, include django.contrib.auth.urls under accounts/.
  • Create notes/templates/registration/login.html: it extends notes/base.html and shows the form in a POST form with a Log in button.
  • A good login lands on /.
  • Under You are not signed in., a link Log 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