django如何全局使用变量 [英] django how to use a variable globally

查看:125
本文介绍了django如何全局使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个视图中有一个变量

I have a variable in one of my view

def ViewName(request):
      simple_variable = request.session['value']

在同一views.py文件中,我有另一个视图

in the same views.py file,i have another view

def AnotherViewName(request):
    --------------------
    --------------------

现在我想在我的 AnotherViewName 视图中使用变量 simple_variable ,我已经尝试过

now i want to use the variable simple_variable in my AnotherViewName view ,i have tried

def AnotherViewName(request):
     global simple_variable

但是它不起作用,现在我的问题是,如何在Django中从一个视图到另一个视图使用变量,或者如何在全局范围内使用变量?

but,its not worked,now my question is,how can i use a variable from one view to another view in Django or how can i use a variable globally?

提及 simple_variable 是存储会话中的值,最初我必须在上面给定的 ViewName 视图中调用它。

in mention the simple_variable is storing value from the session and initially i have to call it within my above given ViewName view.

我正在使用django 1 。 5

i am using django 1.5

推荐答案

我认为您可以这样做:

simple_variable = initial_value 

def ViewName(request):
    global simple_variable
    simple_variable = value
    ...

def AnotherViewName(request):
    global simple_variable

这篇关于django如何全局使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆