Private by Default
Part of the Users and Accounts section of Coddy's Django journey. Lesson 7 of 16.
A notes app is private: almost nothing should be open to visitors. With @login_required on every view, the day you forget it on a new view, that page is public.
Middleware is code every request passes through on its way to a view; MIDDLEWARE in settings.py lists it in order. LoginRequiredMiddleware sends every visitor who is not signed in to LOGIN_URL, whatever page they asked for. It reads request.user, so it goes after AuthenticationMiddleware, which sets it.
Then you mark the pages that stay public. A members' club keeps its opening hours open to anyone:
from django.contrib.auth.decorators import login_not_required
@login_not_required
def opening_hours(request):
return render(request, "club/hours.html")Django's login page is marked already. Every other view is now protected, so the @login_required lines can go.
Challenge
Easy- Turn on
"django.contrib.auth.middleware.LoginRequiredMiddleware"inMIDDLEWARE, in the right place. - Remove the three
@login_requiredlines. - A visitor can still open
/signup/and make an account.
Press Run: / opens on the login page. Then follow the Sign up link.
Try it yourself
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = "django-insecure-change-me"
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"notes",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "mysite.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
AUTH_PASSWORD_VALIDATORS = [
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
STATIC_URL = "static/"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Users and Accounts
Practice on your own: Django playground