Skip to main content

Requires login to all requests through middleware.

Project description

django-login-required-middleware

Tests Coverage Status PyPI Version PyPI downloads

django-login-required-middleware provide login to all requests through middleware.

If the website has many views and almost all use LoginRequiredMixin or the login_required decorator, using django-login-required can keep the code of your views more clear and avoids forgetting authentication of view.

Requirements

  • Python: 3.6, 3.7, 3.8
  • Django: 1.11, 2.0, 2.1, 2.2, 3.0x

Quick start

  1. Install pip install django-login-required-middleware

  2. Add login_required.middleware.LoginRequiredMiddleware to MIDDLEWARE after django.contrib.auth.middleware.AuthenticationMiddleware

  3. (Optional) To ignore authentication in a view uses decorato @login_not_required for FBV or LoginNotRequiredMixin for CBV:

    from login_required import login_not_required
    
    @login_not_required
    def my_view(request):
        return HttpResponse()
    

    or

    from login_required import LoginNotRequiredMixin
    
    class MyView(LoginNotRequiredMixin, View):
        def get(self, request, *args, **kwargs):
            return HttpResponse()
    
  4. (Optional) Add LOGIN_REQUIRED_IGNORE_PATHS setting. Any requests which match these paths will be ignored. This setting should be a list filled with regex paths (settings.LOGIN_URL always included).

    Example:

    LOGIN_REQUIRED_IGNORE_PATHS = [
        r'/accounts/logout/$'
        r'/accounts/signup/$',
        r'/admin/$',
        r'/about/$'
    ]
    
  5. (Optional) Add LOGIN_REQUIRED_IGNORE_VIEW_NAMES setting. Any requests which match these url name will be ignored. This setting should be a list filled with url names.

    Example:

    LOGIN_REQUIRED_IGNORE_VIEW_NAMES = [
        'home',
        'admin:index',
        'admin:login',
        'namespace:url_name',
    ]
    
  6. (Optional) Add LOGIN_REQUIRED_REDIRECT_FIELD_NAME setting. This will be passed to Django's redirect_to_login(). Default is 'next'.

    Example:

    LOGIN_REQUIRED_REDIRECT_FIELD_NAME = 'next_url'
    

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

File details

Details for the file django-login-required-middleware-0.8.0.tar.gz.

File metadata

File hashes

Hashes for django-login-required-middleware-0.8.0.tar.gz
Algorithm Hash digest
SHA256 696558388cd4dd718fb1d81f5ee95318c148743fc1e90e5d6d6c6363c4e9c818
MD5 c77c853b81e3ece0e7c2252ea63cbae1
BLAKE2b-256 25db4e312f5c8b5a2b863e19fec5b7424c4ef0e191c5f5252a52432348185147

See more details on using hashes here.

File details

Details for the file django_login_required_middleware-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_login_required_middleware-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29039c6a91df5928383f6f892496d576ad27d2210cbb6f92f6edd73c95471a83
MD5 6d918c525f57cb852bf1de4770498cc0
BLAKE2b-256 f5ba0cdcc46ecdf20fe4b24de7364df03f74e1b5dbea1b27503271e43b237cbf

See more details on using hashes here.

Supported by

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