Home > Mobile >  how to pass variables between functions Django?
how to pass variables between functions Django?

Time:07-12

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?

CodePudding user response:

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)
  • Related