【发布时间】:2012-09-30 11:06:15
【问题描述】:
在views.py中
当我尝试从其他def 访问全局变量时:
def start(request):
global num
num=5
return HttpResponse("num= %d" %num) # returns 5 no problem....
def other(request):
num=num+1
return HttpResponse("num= %d" %num)
def other 不返回 6,但应该是 6 对吧?如何在我的视图中全局访问变量?
【问题讨论】:
-
在你想在全局范围内使用
num的所有函数中添加global num -
您可以按照答案中的说明进行操作,也可以将变量数据添加到数据库中并在全局范围内使用该数据。
标签: python django django-views