Skip to main content

django-login-default

A Django middleware that makes login required the default. Instead of decorating every view with @login_required, you add one middleware and then mark the exceptions — the views that should be public.

Requires Django 4.2+ and Python 3.10+.

Installation

pip install django-login-default

Usage

1. Add the middleware

In your settings.py, add LoginRequiredMiddleware after AuthenticationMiddleware:

INSTALLED_APPS = [
    ...
    "django_login_default",
]

MIDDLEWARE = [
    ...
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_login_default.middleware.LoginRequiredMiddleware",
    ...
]

LOGIN_URL = "/accounts/login/"

That's it — every view now requires an authenticated user. Unauthenticated requests get redirected to LOGIN_URL with a ?next= parameter.

2. Mark public views

For views that should be accessible without login, use the decorator (FBV) or the mixin (CBV):

# Function-based view
from django_login_default.decorators import no_login_required

@no_login_required
def landing_page(request):
    ...
# Class-based view
from django_login_default.mixins import NoLoginRequiredMixin

class LandingPage(NoLoginRequiredMixin, TemplateView):
    template_name = "landing.html"

How it works

The middleware runs on every request. If the user is not authenticated, it resolves the URL and checks the view for a no_login_required attribute. If the attribute is present (set by the decorator or mixin), the request passes through. Otherwise, it redirects to the login page.


Development

Setup

git clone https://github.com/mbaer/django-login-default.git
cd django-login-default
python -m venv .venv
source .venv/bin/activate
pip install -e .

Sample project

There's a small Django project in sample_project/ that demonstrates the middleware. It has a protected page, a public page, and a login form.

cd sample_project
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Running tests

# With coverage
PYTHONPATH=sample_project:$PYTHONPATH \
  coverage run --source=django_login_default -m django test django_login_default \
  --settings=sample_project.settings
coverage report

# Across multiple Python/Django versions
pip install tox
tox

Project structure

django_login_default/
├── middleware.py      # LoginRequiredMiddleware
├── decorators.py      # @no_login_required
├── mixins.py          # NoLoginRequiredMixin
├── views.py           # Demo views used by the sample project
└── tests/
    └── test_middleware.py

License

MIT

Metadata

Release files for django-login-default 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-login-default 0.1.2
File Size Uploaded
django_login_default-0.1.2.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-login-default 0.1.2
File Interpreter ABI Platform
django_login_default-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 23.5 kB

Release files / django_login_default-0.1.2.tar.gz

Download URL django_login_default-0.1.2.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a04503bab83ab0f8573940c7e626df91138976391d76923d8baa2e15d2d6b90a
BLAKE2b-256 checksum
How to use checksums
c909ab8bf8a13914843d5d51b7751b08bba7bca3dbe4c1bd7b182d7d0539d6aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / django_login_default-0.1.2-py3-none-any.whl

Download URL django_login_default-0.1.2-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c83a926f17b8e76d92244c8ab7747eedd0bbc2ea8a261e7861ce96bd0851d2c3
BLAKE2b-256 checksum
How to use checksums
9e36104c3116cc3e9f7333b2adb90f6d481caa3c1bc1f0675554078c8b48b27c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page