gpt4 book ai didi

python - 如何在 Django 函数之间传递变量?

转载 作者:行者123 更新时间:2023-12-05 03:21:32 25 4
gpt4 key购买 nike

我在 views.py 中有这样一个函数:

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

然后我有另一个这样的函数:

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!')

我想使用第一个函数的“uname”变量,但它不起作用任何解决方案?

最佳答案

在第一个 View 中,将值保存在session中:

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

然后在第二个 View 中,从 session 中获取值:

def reservations(request):
try:
uname = request.session['uname']
c = Utilisateur.objects.get(username = uname)

关于python - 如何在 Django 函数之间传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72945219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com