Skip to main content

Django middleware that makes login required by default

Project description

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

Project details


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.1.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.1-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_login_default-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 b3e1e1db2c862bf8165620304079d800f34092366e79df27aa8bde6807fbae44
MD5 8893923ebd02bf32e9106d6b863dce5e
BLAKE2b-256 d81188603315b87c1579021c50c8ad9365243595c90cc4ce66d44fbbc5c84f2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_login_default-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d739bae8ad7038ffc5d5b7c9e4e0f7ddd2b93fa96d32d416adb4f8524f913e6
MD5 545b135ef008e75e4f7d1d746b396294
BLAKE2b-256 549e7438e7fa28bc23d03c84615bedd0e85cd1affce4680b1c6091860d188712

See more details on using hashes here.

Supported by

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