I set up a payment gateway and use sessions to store data in all page requests. The following class is used to organize and store information in a session.
class Gateway: def __init__(self, session_key=None, session_name="FOO"):
This view checks if the user is logged in. If he / she is redirected. If not, he will be asked to either log in or register as a guest.
def check_in(request): gateway = Gateway(session_key=request.session.session_key) if request.user.is_authenticated(): gateway.user_id = request.user.id gateway.checked_in = True gateway.save() return redirect('next_step') else: login_form = FormLogin() if request.POST: data = request.POST.copy() if 'login' in data: login_form = FormLogin(data) if login_form.is_valid(): user = login(request, login_form) if user: gateway.user_id = user.id gateway.checked_in = True gateway.save() return redirect('next_step') elif 'guest' in data: gateway.checked_in = True gateway.save() return redirect('next_step') return render( request, 'shop/login.html', { 'login_form':login_form, } )
The following view checks the variable "checked_in". This is to ensure that users do not miss the login / registration process. (As an additional note, the "login (request, login_form)" function is a function that works fine in other contexts and returns the user if it was successful, and None otherwise)
def next_step(request): gateway = Gateway(session_key=request.session.session_key) if not gateway.checked_in:
Now for the problem:
Even when the user is authenticated, the variable "checked_in" is still false and causes a loop to be viewed. A new session with a new session identifier is created every time I set a variable and save it. There are some explanations in the django docs about modifying sessions, but I cannot understand why a new session is being created or why the session key is changing.
edit: I am using a database backend.
python django cookies session-cookies session
Quinton robbins
source share