In views.py
When I try this one to access a global variable from other 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 does not return 6, but it should be 6 right ? How can I access a variable globally in my view ?
Use sessions. This is exactly what they are designed for.
If the session has expired, or
numdid not exist in the session (was not set) thenrequest.session.get('num')will returnNone. If you want to givenuma default value, then you can do thisrequest.session.get('num',5)– now ifnumwasn’t set in the session, it will default to5. Of course when you do that, you don’t need theif num is Nonecheck.