如何使用字符串[django]访问python模块变量 [英] How to access a python module variable using a string [ django ]
本文介绍了如何使用字符串[django]访问python模块变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个django应用程序,其模块名为"app"
现在,该模块具有一个名为"urls.py" 的文件,该文件具有一个名为"HOME_URL "
I have a django application, with a module called "app"
Now this module has a file called "urls.py" which has a variable called "HOME_URL"
我要做什么?
app_label = "app"
url = __import__(app_label).urls.HOME_URL
print url
那显然是行不通的,但是我希望你能得到我想要做的.如果没有,请发表评论,我将编辑问题以包含更多信息.
That obviously doesn't work, but I hope you got what I'm trying to do, If not please comment I will edit the question to contain more info.
推荐答案
You can use import_module
to load a module relative to the root of a django project.
from django.utils.importlib import import_module
app_label = "app"
url = import_module("%s.urls" % app_label).HOME_URL
这应该在您的django项目中或在 ./manage.py shell
中工作.
This should work within your django project, or in ./manage.py shell
.
这篇关于如何使用字符串[django]访问python模块变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文