login_required decorator that doesn't redirect

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from functools import wraps  # adapt if you need python 2.4 support

from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import login


def login_required(view_callable):
    def check_login(request, *args, **kwargs):
        if request.user.is_authenticated():
            return view_callable(request, *args, **kwargs)

        assert hasattr(request, 'session'), "Session middleware needed."
        login_kwargs = {
            'extra_context': {
                REDIRECT_FIELD_NAME: request.get_full_path(),
            },
        }
        return login(request, **login_kwargs)
    return wraps(view_callable)(check_login)

More like this

  1. is_staff decorator by munhitsu 4 years, 9 months ago
  2. LoginRequired class-based view decorator by mjumbe 1 year, 10 months ago
  3. Login Required Middleware with Next Parameter by bernardoporto 6 months, 1 week ago
  4. More information about users and groups in user admin by ramusus 3 years, 10 months ago
  5. HTTP basic auth decorator by bthomas 4 years, 3 months ago

Comments

(Forgotten your password?)