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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_login_default-0.1.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_login_default-0.1.2-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file django_login_default-0.1.2.tar.gz.

File metadata

  • Download URL: django_login_default-0.1.2.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for django_login_default-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a04503bab83ab0f8573940c7e626df91138976391d76923d8baa2e15d2d6b90a
MD5 92b87611c8572023a8abd00a032b343b
BLAKE2b-256 c909ab8bf8a13914843d5d51b7751b08bba7bca3dbe4c1bd7b182d7d0539d6aa

See more details on using hashes here.

File details

Details for the file django_login_default-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_login_default-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c83a926f17b8e76d92244c8ab7747eedd0bbc2ea8a261e7861ce96bd0851d2c3
MD5 9f6882b1cd4d6e88035037a00cf3e0ea
BLAKE2b-256 9e36104c3116cc3e9f7333b2adb90f6d481caa3c1bc1f0675554078c8b48b27c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page