其他分享
首页 > 其他分享> > Django补充

Django补充

作者:互联网

目录

django配置文件相关操作

django实际上有两个配置文件

一个是提供给用户可以自定义的基本配置
	from 项目名 import settings
一个是全局的系统默认的配置
	from django.conf import global_settings
  提示:用户不指定的情况下采用全局的系统默认的配置

配置文件使用的优先级

用户如果在提供给用户的配置文件中指定了配置就使用用户指定的配置,不指定的情况下则使用系统默认的配置

django两个配置文件的关系

提供给用户的配置文件中有的配置,系统配置文件中肯定有
提供给用户的配置文件中没有的配置,系统配置文件中也有

在django中如果想要同时使用两个配置文件的导入方式

from django.conf import settings

基于django配置文件源码编写代码

os.environ

可看成是一个项目全局的大字典,任何地方都可以使用

django的入口文件是manage.py

可看成是启动文件

os.environ.setdefault()

相当于给字典添加键值对

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day67.settings")
print(settings.BASE_DIR)  # C:\D_disk\PythonProject\week14\day67,没有添加会报错django.core.exceptions.ImproperlyConfigured: Requested setting BASE_DIR, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

源码分析

class LazySettings(LazyObject):
    """
    A lazy proxy for either global Django settings or a custom settings object.
    The user can manually configure settings prior to using them. Otherwise,
    Django uses the settings module pointed to by DJANGO_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)  # 获取提供给用户的配置文件字符串路径 'day67.settings'
        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)   # 将上述字符串路径当做参数传给Settings类实例化

class Settings(object):
    def __init__(self, settings_module):
        # update this dict from global settings (but only for ALL_CAPS settings)
        for setting in dir(global_settings):  # 获取系统配置文件中所有的变量名
            if setting.isupper():   # 校验变量名是否是纯大写
                setattr(self, setting, getattr(global_settings, setting))  # 给settings对象赋值通过反射获取到的系统配置文件中所有的大写变量名和对应的值

        # store the settings module in case someone later cares
        self.SETTINGS_MODULE = settings_module  # 存储设置模块,后面可能用到

        mod = importlib.import_module(self.SETTINGS_MODULE)

        tuple_settings = (
            "INSTALLED_APPS",
            "TEMPLATE_DIRS",
            "LOCALE_PATHS",
        )
        self._explicit_settings = set()
        for setting in dir(mod):   # 循环获取提供给用户的配置文件中所有的变量名
            if setting.isupper():
                setting_value = getattr(mod, setting)  # 给对象设置属性

基于setting源码编程

# 启动文件
import sys
import os


sys.path.append(os.path.dirname(__file__))
if __name__ == '__main__':
    os.environ.setdefault('user_setting', 'conf.settings')
    from conf import settings
    print(settings.ATTRIBUTE_NAME)
    from lib.conf import global_settings
    print(global_settings.ATTRIBUTE_NAME)
    from lib.conf import setting
    print(setting.ATTRIBUTE_NAME)
# 用户配置文件
ATTRIBUTE_NAME = '用户设置中的属性'
# 全局配置文件
ATTRIBUTE_NAME = '全局设置中的属性'
# init__.py
import importlib
import os
from lib.conf import global_settings


class Settings(object):
    def __init__(self):
        for setting_name in dir(global_settings):
            if setting_name.isupper():
                values = getattr(global_settings, setting_name)
                setattr(self, setting_name, values)
        user_module_path = os.environ.get('user_setting')
        settings = importlib.import_module(user_module_path)
        for name in dir(settings):
            if name.isupper():
                settings_values = getattr(settings, name)
                setattr(self, name, settings_values)


setting = Settings()

权限管理简介


基于角色的权限管理(RBAC)


admin配置类重要参数


admin源码及路由分发本质



标签:name,配置文件,settings,补充,django,setting,import,Django
来源: https://www.cnblogs.com/longlive/p/16338806.html