安装 Django
pip install Django
https://docs.djangoproject.com
查看 当前环境 django 版本python -m django --version
创建项目django-admin startproject mysite
创建应用python manage.py startapp polls
安装阿里云 OSS 第三方<br />pip install django-oss-storage
drfpip install djangorestframework
Pillow
pip install Pillow
设置语言时区
设置STATIC_ROOT
设置阿里云 OSS
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
UPLOAD_ROOT = '/'
# 阿里云
ACCESS_KEY_ID = "XXXXXX"
ACCESS_KEY_SECRET = "XXXXXX"
END_POINT = "https://oss-cn-XXXXXX.aliyuncs.com"
BUCKET_NAME = "XXXXXX"
# 绑定域名
ALIYUN_OSS_CNAME = "https://xxx.xxx.xxx.com"
BUCKET_ACL_TYPE = "public-read" # private, public-read, public-read-write
DEFAULT_FILE_STORAGE = 'libs.oss_storage.AliyunMediaStorage'
python manage.py collectstatic
同步静态文件
配置生产与测试 mysql 环境
创建 不同环境 配置文件
import logging
logger = logging.getLogger(__name__)
# Detect environment
environment = os.environ.get('TT_AUDIO_ENV')
if environment == 'LOCALE':
logger.info('Load locale environment settings')
from .env_conf.locale import *
elif environment == 'PRODUCT':
logger.info('Load product environment settings')
from .env_conf.product import *
安装 mysql 与 mysqlclientbrew install mysql
pip install mysqlclient
启动 mysqlmysql.server start
进入 mysql 创建 数据库mysql -uroot -p
CREATE DATABASE [数据库名];
配置本地 locale.py 数据库设置
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名',
'USER': 'root',
'PASSWORD': '密码',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
},
},
}
然后删除 settings.py 的数据库配置
执行 migratepython manage.py migrate
创建管理员账号
python manage.py createsuperuser