stream886

配置文件

在新建项目中,文件 settings.py 为项目的配置文件,现在详细解读下整个配置文件。

以下为原生的配置文件内容

import os

# 项目根路径
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 安全秘钥
SECRET_KEY = \'&6ry6mx#9s*%s@w(d$z+pinn0ft8r+vj96^9mhzv7y*sruyfhy\'

# DEBUG配置为True的时候会暴露出一些出错信息或者配置信息以方便调试.但是在上线的时候应该将其关掉,防止配置信息或者敏感出错信息泄露.
DEBUG = True

# 允许访问的主机,如果填入*号,则允许所有人访问
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = [\'*\']

# Application definition: 一个一元数组,里面是应用中要加载的自带或者自己定制的app包路径列表.
INSTALLED_APPS = [
    \'django.contrib.admin\',
    \'django.contrib.auth\',
    \'django.contrib.contenttypes\',
    \'django.contrib.sessions\',
    \'django.contrib.messages\',
    \'django.contrib.staticfiles\'
]

# 中间件
MIDDLEWARE = [
    \'django.middleware.security.SecurityMiddleware\',
    \'django.contrib.sessions.middleware.SessionMiddleware\',
    \'corsheaders.middleware.CorsMiddleware\',
    \'django.middleware.common.CommonMiddleware\',
    \'django.middleware.csrf.CsrfViewMiddleware\',
    \'django.contrib.auth.middleware.AuthenticationMiddleware\',
    \'django.contrib.messages.middleware.MessageMiddleware\',
    \'django.middleware.clickjacking.XFrameOptionsMiddleware\',
]

CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = \'justdoit.urls\'

# 模板配置,修改DIRS,指定为自己创建的模板文件夹,使用pycharm创建项目时,这步自动完成
TEMPLATES = [
    {
        \'BACKEND\': \'django.template.backends.django.DjangoTemplates\',
        \'DIRS\': [os.path.join(BASE_DIR, \'templates\')],
        \'APP_DIRS\': True,
        \'OPTIONS\': {
            \'context_processors\': [
                \'django.template.context_processors.debug\',
                \'django.template.context_processors.request\',
                \'django.contrib.auth.context_processors.auth\',
                \'django.contrib.messages.context_processors.messages\',
            ],
        },
    },
]

WSGI_APPLICATION = \'justdoit.wsgi.application\'

# Database 数据库配置,默认为sqlite3
DATABASES = {
    \'default\': {
        \'ENGINE\': \'django.db.backends.sqlite3\',
        \'NAME\': os.path.join(BASE_DIR, \'db.sqlite3\'),
    }
}

# Password validation 密码验证
AUTH_PASSWORD_VALIDATORS = [
    {
        \'NAME\': \'django.contrib.auth.password_validation.UserAttributeSimilarityValidator\',
    },
    {
        \'NAME\': \'django.contrib.auth.password_validation.MinimumLengthValidator\',
    },
    {
        \'NAME\': \'django.contrib.auth.password_validation.CommonPasswordValidator\',
    },
    {
        \'NAME\': \'django.contrib.auth.password_validation.NumericPasswordValidator\',
    },
]


# Internationalization 国际化
# 语言设置,已更改为中文
LANGUAGE_CODE = \'zh-Hans\'
# 市区设置,已更改为上海
TIME_ZONE = \'Asia/Shanghai\'

USE_I18N = True

USE_L10N = True

USE_TZ = True

总结几个简单的点:

  • DEBUG的开启和关闭
  • ALLOWED_HOSTS访问主机的开放
  • LANGUAGE_CODO语言设置中文
  • TIME_ZONE时区设置

数据库配置DATABASE — 指定MySQL数据库

  • 数据库配置,默认设置为sqlite,以下修改为MySQL

    DATABASES = {
        \'default\': {
            \'ENGINE\': \'django.db.backends.mysql\',
            "HOST": \'172.17.180.XXX\',
            \'NAME\': \'DATABASE_NAME\',
            \'USER\': \'name\',
            \'PASSWORD\': \'password\',
        },
    }
    
  • 改用pymysql驱动,在init.py文件中加入一下代码:

    import pymysql
    pymysql.install_as_MySQLdb()
    

数据库配置DATABASE — 指定SQL Server数据库

  • django-pyodbc-azure 2.0.1.0

    Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc

    依赖于pyodbc,但pyodbc要驱动SQL Server还想再安装微软的驱动

    pip install django-pyodbc-azure
    
  • Microsoft ODBC Driver for SQL Server

    Window :直接安装微软家的IDE后,自动ok

    Mac : 比较麻烦

    /usr/bin/ruby -e "$(curl -fsSL      https://raw.githubusercontent.com/Homebrew/install/master/install)"
    brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
    brew update
    brew install --no-sandbox msodbcsql mssql-tools
    
  • brew install unixodbc

    Mac下除了驱动安装麻烦,还需要安装unixodbc后,pyodbc后才可以使用,如果import pyodbc失败,再试试 brew link unixodbc

  • 配置settings.py

    DATABASES = {
        \'default\':{
            \'ENGINE\':\'sql_server.pyodbc\',
            \'HOST\':\'172.17.180.XXX\',
            \'NAME\':\'DATABASE_NAME\',
            \'USER\':\'name\',
            \'PASSWORD\':\'password\',
        }
    }
    

数据库配置DATABASE — 指定多个数据库

模版文件

  • 模板配置,修改DIRS,指定为自己创建的模板文件夹,使用pycharm创建项目时,这步自动完成

    TEMPLATES = [
        {
            \'BACKEND\': \'django.template.backends.django.DjangoTemplates\',
            \'DIRS\': [os.path.join(BASE_DIR, \'templates\')],  #根目录下的templates文件夹
            \'APP_DIRS\': True,  #每个APP下的templates文件夹
            \'OPTIONS\': {
                \'context_processors\': [
                    \'django.template.context_processors.debug\',
                    \'django.template.context_processors.request\',
                    \'django.contrib.auth.context_processors.auth\',
                    \'django.contrib.messages.context_processors.messages\',
                ],
            },
        },
    ]
    

静态文件

Static

  1. 在项目根目录新建static文件夹

  2. 在setting.py中设置:

    STATIC_URL=\'/static/\'  #HTML中使用的静态文件夹前缀
    STATIC_DIR=os.path.join(BASE_DIR,\'static\') 
    STATICFILES_DIRS=[STATIC_DIR,]  #当由多个静态文件目录时,可加入
    STATIC_ROOT=STATICFILES_DIRS
    

    static

  3. 在模板文件,写入{% load static %}

  4. 在模板文件中引用:{% static "/assets/js/jquery.min.js" %}

媒体文件

Media

  1. 在项目根目录新建media文件夹

  2. 在setting.py中设置

  3. MEDIA_URL=\'/media/\'  #前后都有斜杠
    MEDIA_DIR=os.path.join(BASE_DIR,\'media\')
    MEDIAFILES_DIRS=[MEDIA_DIR,]
    MEDIA_ROOT=MEDIA_DIR
    

    TEMPLATES设置中的context_processor选项中加入:\'django.template.context_processors.media\',

  4. 在项目urls文件中

    from django.conf import settings
    from django.conf.urls.static import static
    

    在文件最后加上:+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

分类:

技术点:

相关文章: