English 中文(简体)
Django :从环境中进口变量更好。 原始档案或基本配置档案?
原标题:Django : is it better to import variables from a settings.py file, or basic configuration file?

我很想知道,从环境中进口变量是否更好。 原始档案? 或更好地编造一个包含所需变量的组合文件?

我倾向于为我的Django申请撰写组合文件,读到,必要时从那里进口变量。 例如:

configrc

[awesomeVariables]
someMagicNumber = 7

<>views.py

from ConfigParser import SafeConfigParser
#Open and parse the file
config = SafeConfigParser()
config.read(pathToConfig)
#Get variable
magicNumber   = config.get( awesomeVariables ,  someMagicNumber ))

www.un.org/Depts/DGACM/index_french.htm

http://www.py。

SOME_MAGIC_NUMBER=7

<>views.py

import settings
magicNumber = settings.SOME_MAGIC_NUMBER

我很想知道不同方法的利弊? 直接从您的环境下进口变量能否损害结构的完整性?

最佳回答

判决要求。 使用环境模块是“詹戈方式”,尽管你们应当使用<条码>,从django开始。 • 配置进口环境; 缩略语 DJANGO_SETMENTS_MODULE 。 但Django只是灰色;没有任何东西可以阻止你使用<条码>。 为了确定此类物品只有一个地方,我建议将其放在Django环境档案中,但如果你有理由不这样做,则不这样做。

问题回答

使用一个混凝土文档完全是非詹戈。 时间。 就特定环境而言,你只是在你看来设定了违约行为,允许用户在其项目环境中凌驾。 py:

from django.conf import settings

SOME_MAGIC_NUMBER = settings.SOME_MAGIC_NUMBER if hasattr(settings,  SOME_MAGIC_NUMBER ) else 0
# Where `0` is the default value

There also an app for storing settings dynamically in db. Maybe you find it usefull . django-constance





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签