Help solve the puzzle. When switching between templates, user authorization data flies. For authorization django.contrib.auth.backends.RemoteUserBackend is used. Here is a sketch of the code. The user is indexed and authorized. Then the already authorized user goes to photo and that's it, he is not authenticated.

def index(request): username = request.environ.get('HTTP_REMOTE_USER') auth = RemoteUserBackend() user = auth.authenticate(request, username) if user is not None: login(request, user, backend='django.contrib.auth') else: pass var = user return render(request, 'core/index.html', {'var': var} @login_required() def photo(request): var = dict() var['some_var'] = 'some data' return render(request, 'core/gallery.html', {'vars': var}) 

How in this case "transfer authorization" to another template?

    2 answers 2

    Is the following in the settings.py file?

     MIDDLEWARE = [ '...', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.RemoteUserMiddleware', '...', ] 
    • Yes, spelled out as in your example. - Evgeny Atanov

    Through experimentation, I discovered the reason for this behavior. Error in this section login(request, user, backend='django.contrib.auth')

    The correct login () should be like this:

     login(request, user, backend='django.contrib.auth.backends.ModelBackend')