django具体设置应用程序 [英] django specific settings app

查看:200
本文介绍了django具体设置应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个django应用程序,需要一个目录来下载和存储文件。



我想保持我的应用程序可重用,所以我不想硬代码该目录的路径。
所以我想让这个路径设置/一个可以设置的全局变量。



我可以在哪里放这个设置/全局变量? p>

这种做法好吗?
http:// blog .muhuk.com / 2010/01/26 / developers-reusable-django-apps-app-settings.html



感谢您的建议! p>

解决方案

我使用以下方法:

 #你的应用程序中的一些文件:

从django.conf导入设置

MY_APP_SETTING = getattr(设置,'MY_APP_SETTING','一些默认值')

这有效地允许最终用户在自己的settings.py中定制设置,但仍然确保总是一些默认值设置。您现在可以在其余代码中随意使用 MY_APP_SETTING



更新



您的问题中的链接加载时间太长,所以我刚刚回来。事实证明,我建议的方法与建议的方法一样,所以是的,我认为这种方法很好;)。


I am working on a django app that needs a directory to download and store files.

I want to keep my app reusable so I do not want to hard code the path of this directory. So I want to make this path a setting/a global variable that can be set up.

Where could I put this setting/global variable?

Is this kind of approach good ? http://blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html

Thanks for your advice!

解决方案

I use the following methodology:

# some file in your app:

from django.conf import settings

MY_APP_SETTING = getattr(settings, 'MY_APP_SETTING', 'some default value')

This effectively allows end-users to customized the setting in their own settings.py, but still ensures that there's always some default value set. You can now use MY_APP_SETTING at will in the rest of your code.

UPDATE

The link in your question was taking too long to load, so I just went ahead and answered. As it turns out, the method I suggested is the same as what it suggests, so yes, I'd consider that approach good ;).

这篇关于django具体设置应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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