Middleware to require login for all Django URLs
Project description
Django Require Login
Forked from django-stronghold
Require login on all your django URLs by default
Supported Versions
- Python 3.5, 3.6, 3.7
- Django 1.11, 2.0, 2.1, 2.2
Installation
Install via pip.
pip install django-require-login
Then add the middleware to your MIDDLEWARE_CLASSES in your Django settings file
MIDDLEWARE_CLASSES = (
#...
"django_require_login.middleware.LoginRequiredMiddleware",
)
Usage
If you followed the installation instructions now all your views are defaulting to require a login. To make a view public again you can use the public decorator:
For function based views
from django_require_login.decorators import public
from django.http import HttpResponse
@public
def my_view(request):
return HttpResponse("Public")
For class based views (decorator)
from django.utils.decorators import method_decorator
from django_require_login.decorators import public
from django.views.generic import View
from django.http import HttpResponse
class SomeView(View):
def get(self, request, *args, **kwargs):
return HttpResponse("Public view")
@method_decorator(public)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
For class based views (mixin)
from django_require_login.mixins import PublicViewMixin
from django.views.generic import View
class SomeView(PublicViewMixin, View):
pass
Configuration (optional)
You can add a tuple of url regexes in your settings file with the
REQUIRE_LOGIN_PUBLIC_URLS
setting. Any url that matches against these patterns
will be made public without using the @public
decorator.
REQUIRE_LOGIN_PUBLIC_URLS
Default:
REQUIRE_LOGIN_PUBLIC_URLS = ()
If DEBUG is True, REQUIRE_LOGIN_PUBLIC_URLS contains:
from django.conf import settings
(
r'{}.+$'.format(settings.STATIC_URL),
r'{}.+$'.format(settings.MEDIA_URL),
)
When settings.DEBUG = True, this is additive to your settings to support serving
Static files and media files from the development server. It does not replace any
settings you may have in REQUIRE_LOGIN_PUBLIC_URLS
.
Note: Public URL regexes are matched against HttpRequest.path_info.
REQUIRE_LOGIN_PUBLIC_NAMED_URLS
You can add a tuple of url names in your settings file with the
REQUIRE_LOGIN_PUBLIC_NAMED_URLS
setting. Names in this setting will be reversed using
django.urls.reverse
and any url matching the output of the reverse
call will be made public without using the @public
decorator:
Default:
REQUIRE_LOGIN_PUBLIC_NAMED_URLS = ()
REQUIRE_LOGIN_USER_TEST_FUNC
Optionally, set REQUIRE_LOGIN_USER_TEST_FUNC to a callable to limit access to users
that pass a custom test. The callback receives a User
object and should
return True
if the user is authorized. This is equivalent to decorating a
view with user_passes_test
.
Example:
REQUIRE_LOGIN_USER_TEST_FUNC = lambda user: user.is_staff
Default:
REQUIRE_LOGIN_USER_TEST_FUNC = lambda user: user.is_authenticated
Security
If you believe you've found a bug with security implications, please do not disclose this issue in a public forum.
Email us at support@laac.dev
Contribute
See CONTRIBUTING.md
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
Built Distribution
Hashes for django_require_login-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04a0ec5cf7dbfa35cacf9bdf256a04bab827953d064a8c08016df215927fc2b5 |
|
MD5 | 265c391ad6f0d72fc1500789e2f90536 |
|
BLAKE2b-256 | 8b8bfb5ba8e230bf9066f6236974905bb4f93d3e7563e1b5a66c25ac29d1f9e6 |
Hashes for django_require_login-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4749fc18ddd9b072a029b52ca57a62b2d35ce7cd86b5c51f2722331745b3a551 |
|
MD5 | c354d4622be985a5bdb40cd4576ab3c8 |
|
BLAKE2b-256 | d0754461044748b9038e8eb148c9d70f0a1cd7ec1c5222b3b9604f776433330b |