D
D
Dauren S2016-07-29 09:04:37
Django
Dauren S, 2016-07-29 09:04:37

Django set default session key value?

When the site loads, I set the default session key in the home method. But I need to set it not only in the home method, but in all methods. How to do it globally? To not write in each method?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Kitaev, 2016-07-29
@dauren101

Something like this:

class SessionModeMiddleware(object):
    def process_request(self, request):
        if 'mode' not in request.session:
            request.session['mode']  = 'm'
            request.session.save()

S
sim3x, 2016-07-29
@sim3x

write a function and call it in every view

T
TheML9I, 2016-07-30
@khaletskiy

As an option, you could extend django.contrib.sessions.middleware.SessionMiddleware and use your own instead of the Django midvari. But turning it off is inconvenient.
You can write your own and connect, like the answer above. And it's ok within django.
If you don’t want magic, we write a decorator on the views and already set the key there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question