3

I have a function like this in views.py:

def signin(request):
    if request.method == 'POST':
        uname = request.POST['username']
        pwd = request.POST['password'] 
        #and other code

And then i have another function like this:

def reservations(request):
    try:
        c = Utilisateur.objects.get(username = uname)
        reserve = Reserve.objects.get(client = c)
        return render (request, 'reservation.html', {'reserve':reserve})
    except:
        return HttpResponse ('you have no reservations!')

And i want to use the "uname" variable of the first function but it doesn't work any solution?

4
  • cant you use a sessions to store values session = sessions.Session() Commented Jul 11, 2022 at 22:27
  • idk how to do that can you help me Commented Jul 11, 2022 at 22:30
  • If you're signing in the user in your signin view, then you don't need to pass uname. Assuming you're using the built in django authentication, you can access the User object as request.user any time after they are logged in. Django sets a cookie so it knows who the user is. Commented Jul 11, 2022 at 22:33
  • thats the problem im not using the built in django authentication Commented Jul 11, 2022 at 22:38

1 Answer 1

3

In the first view, save the value in the session:

def signin(request):
    if request.method == 'POST':
        uname = request.POST['username']
        request.session['uname'] = uname

Then in the second view, fetch the value from the session:

def reservations(request):
    try:
        uname = request.session['uname']
        c = Utilisateur.objects.get(username = uname)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.