python - Create variables in Django views.py and access them at later point -
let's have following 2 functions in views.py:
def foo(request): class_instance = someclass() return httpresponse('whatever') def bar(request): # here want access class_instance initially, foo() called, , @ later point bar() called, in want access variable class_instance.
is there way of accomplishing while neither using request.session nor django models (e.g. django.db.models)?
(nb: had luck global variables in views.py, bad idea anyway , somehow didn't work)
you can put variable module constant, if it's not prone changing much.
class_instance = someclass() def foo(request): return httpresponse('whatever') def bar(request): # here want access class_instance then both methods have access that, but keep in mind, functions can't change constant's value way.
ps: can change value of global:
class_instance = someclass() def foo(request): global class_instance class_instance = someclass() # new instance return httpresponse('whatever') def bar(request): # here want access class_instance
Comments
Post a Comment