L
L
Legalas612019-12-08 20:23:28
Django
Legalas61, 2019-12-08 20:23:28

Is it possible to make a temporary variable for a Django session?

I reinvent the wheel, namely weather parsing. In view.py, when I go to the main page, I parse xml and output to the client.
I would like to throw the variable into request.session after the first parsing and store it until the page is closed. I read how to make an "expiration date" for the session, but I don't need to kill the entire session, but only the session['weather'] variable.
The code

request.session.set_test_cookie()
    if request.session.test_cookie_worked():
        request.session.delete_test_cookie()
        request.session['weather'] = weather
        if request.session['weather']:
            print('Уже есть погода')
    else:
        request.session['weather'] = 'Нормальный'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gh0sty, 2019-12-09
@gh0sty

There is no built-in option, only for the entire session.
Just throw in request.session['weather_expires'] = (DateTime Object). Write down the type when it expires (timezone.now() + timedelta(1) # 1 day), and get into the middleware. Just set your middleware class, throw in the default code from the documentation , and if timezone.now > weather_expires - delete both keys.
Not stsy, et ez.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question