配置django 的环境,django配置静态文件

  配置django 的环境,django配置静态文件

  这篇文章主要介绍了姜戈加载配置的过程详解,包括姜戈服务启动manage.py的详细介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  

目录
一。姜戈服务启动manage.py二。引入配置三。加载配置

  

一. Django服务启动 manage.py

  OS。环境。设置默认值( DJANGO _ SETTINGS _ MODULE , ui.settings )

  设置配置文件环境变量-

  #!/usr/bin/env python

  导入操作系统

  导入系统

  if __name__==__main__:

  OS。环境。设置默认值( DJANGO _ SETTINGS _ MODULE , ui.settings )

  尝试:

  从姜戈.核心.管理导入执行命令行

  除了导入错误:

  #上述导入可能由于其他原因而失败。确保

  #问题是姜戈失踪是为了避免掩盖其他人

  # Python 2上的异常。

  尝试:

  进口框架

  除了导入错误:

  引发导入错误(

  无法导入姜戈.你确定已经安装了吗?

  在路径环境变量中可用。你有吗

  "忘记激活虚拟环境了吗?"

  )

  上升

  执行命令行(sys.argv)

  

二. 引入配置

  django/core/management/init。巴拉圭文件中引入配置

  从django.conf导入设置

  django/conf/init。巴拉圭配置文件源码

  从django.utils.functional导入LazyObject空

  环境变量=姜戈设置模块

  类惰性设置(惰性对象):

  全局姜戈设置或自定义设置对象的惰性代理。

  用户可以在使用之前手动配置设置。否则,

  姜戈使用DJANGO _设置_模块指向的设置模块。

  def _setup(self,name=None):

  加载环境变量指向的设置模块。这

  第一次使用时,我们需要任何设置,如果用户没有

  之前手动配置了设置。

  设置_模块=操作系统。环境。获得(E

  NVIRONMENT_VARIABLE)

   if not settings_module:

   desc = ("setting %s" % name) if name else "settings"

   raise ImproperlyConfigured(

   "Requested %s, but settings are not configured. "

   "You must either define the environment variable %s "

   "or call settings.configure() before accessing settings."

   % (desc, ENVIRONMENT_VARIABLE))

   self._wrapped = Settings(settings_module)

   def __repr__(self):

   # Hardcode the class name as otherwise it yields Settings.

   if self._wrapped is empty:

   return <LazySettings [Unevaluated]>

   return <LazySettings "%(settings_module)s"> % {

   settings_module: self._wrapped.SETTINGS_MODULE,

   }

   def __getattr__(self, name):

   """

   Return the value of a setting and cache it in self.__dict__.

   """

   if self._wrapped is empty:

   self._setup(name)

   val = getattr(self._wrapped, name)

   self.__dict__[name] = val

   return val

   def __setattr__(self, name, value):

   """

   Set the value of setting. Clear all cached values if _wrapped changes

   (@override_settings does this) or clear single values when set.

   """

   if name == _wrapped:

   self.__dict__.clear()

   else:

   self.__dict__.pop(name, None)

   super(LazySettings, self).__setattr__(name, value)

   def __delattr__(self, name):

   """

   Delete a setting and clear it from cache if needed.

   """

   super(LazySettings, self).__delattr__(name)

   self.__dict__.pop(name, None)

   def configure(self, default_settings=global_settings, **options):

   """

   Called to manually configure the settings. The default_settings

   parameter sets where to retrieve any unspecified values from (its

   argument must support attribute access (__getattr__)).

   """

   if self._wrapped is not empty:

   raise RuntimeError(Settings already configured.)

   holder = UserSettingsHolder(default_settings)

   for name, value in options.items():

   setattr(holder, name, value)

   self._wrapped = holder

   @property

   def configured(self):

   """

   Returns True if the settings have already been configured.

   """

   return self._wrapped is not empty

  settings = LazySettings() # 单例模式

  

  LazySettings()惰性加载是一种延迟计算的技术,当只有真正需要使用结果的时候才会去计算。Django提供了两种惰性加载模块,分别是lazy和LazyObject,前者主要针对可以调用的对象,延迟函数的调用;后者针对类,延迟类的实例化。

  如果想要让某个类有延迟实例化的功能,必须做两件事情:

  1)继承LazyObject;
2)实现_setup方法。

  

empty = object()

  def new_method_proxy(func):

   def inner(self, *args):

   if self._wrapped is empty:

   self._setup()

   return func(self._wrapped, *args)

   return inner

  class LazyObject(object):

   """

   A wrapper for another class that can be used to delay instantiation of the

   wrapped class.

   By subclassing, you have the opportunity to intercept and alter the

   instantiation. If you dont need to do that, use SimpleLazyObject.

   """

   # Avoid infinite recursion when tracing __init__ (#19456).

   _wrapped = None

   def __init__(self):

   """

   初始化

   """

   # Note: if a subclass overrides __init__(), it will likely need to

   # override __copy__() and __deepcopy__() as well.

   self._wrapped = empty

   __getattr__ = new_method_proxy(getattr)

   def __setattr__(self, name, value):

   if name == "_wrapped":

   # Assign to __dict__ to avoid infinite __setattr__ loops.

   self.__dict__["_wrapped"] = value

   else:

   if self._wrapped is empty:

   self._setup()

   setattr(self._wrapped, name, value)

   def __delattr__(self, name):

   if name == "_wrapped":

   raise TypeError("cant delete _wrapped.")

   if self._wrapped is empty:

   self._setup()

   delattr(self._wrapped, name)

   def _setup(self):

   """

   Must be implemented by subclasses to initialize the wrapped object.

   """

   raise NotImplementedError(subclasses of LazyObject must provide a _setup() method)

   def __reduce__(self):

   if self._wrapped is empty:

   self._setup()

   return (unpickle_lazyobject, (self._wrapped,))

   def __getstate__(self):

   if self._wrapped is empty:

   self._setup()

   return self._wrapped.__dict__

  

  

  

三. 加载配置

  ManagementUtility 的execute方法的 settings.INSTALLED_APPS

  1) settings.INSTALLED_APPS 因为settings没有INSTALLED_APPS属性就会调用LazySettings的__getattr__方法

  

 def __getattr__(self, name):

   """

   Return the value of a setting and cache it in self.__dict__.

   """

   if self._wrapped is empty:

   self._setup(name)

   val = getattr(self._wrapped, name)

   self.__dict__[name] = val

   return val

  2)self._wrapped is empty(empty是LazyObject的类属性)为True, 就会执行LazySettings的_setup方法,实例self._wrapped =Settings(settings_module)

  

 def _setup(self, name=None):

   """

   Load the settings module pointed to by the environment variable. This

   is used the first time we need any settings at all, if the user has not

   previously configured the settings manually.

   """

   settings_module = os.environ.get(ENVIRONMENT_VARIABLE)

   if not settings_module:

   desc = ("setting %s" % name) if name else "settings"

   raise ImproperlyConfigured(

   "Requested %s, but settings are not configured. "

   "You must either define the environment variable %s "

   "or call settings.configure() before accessing settings."

   % (desc, ENVIRONMENT_VARIABLE))

   self._wrapped = Settings(settings_module)

  3) 后面再访问属性时直接从self._wrapped.dict(settings.wrapped.dict)中获取

  到此这篇关于Django加载配置的过程详解的文章就介绍到这了,更多相关django加载配置内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码:

Copyright @ 2018-2022 盛行IT 合作邮箱: mdzz19960812@outlook.com

备案号:湘ICP备2023015575号