安装 Django

  1. 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
drf
pip install djangorestframework
Pillow
pip install Pillow
设置语言时区
设置STATIC_ROOT
设置阿里云 OSS

  1. LANGUAGE_CODE = 'zh-Hans'
  2. TIME_ZONE = 'Asia/Shanghai'
  3. USE_I18N = True
  4. USE_L10N = True
  5. USE_TZ = False
  6. # Static files (CSS, JavaScript, Images)
  7. # https://docs.djangoproject.com/en/1.11/howto/static-files/
  8. STATIC_URL = '/static/'
  9. STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
  10. UPLOAD_ROOT = '/'
  11. # 阿里云
  12. ACCESS_KEY_ID = "XXXXXX"
  13. ACCESS_KEY_SECRET = "XXXXXX"
  14. END_POINT = "https://oss-cn-XXXXXX.aliyuncs.com"
  15. BUCKET_NAME = "XXXXXX"
  16. # 绑定域名
  17. ALIYUN_OSS_CNAME = "https://xxx.xxx.xxx.com"
  18. BUCKET_ACL_TYPE = "public-read" # private, public-read, public-read-write
  19. DEFAULT_FILE_STORAGE = 'libs.oss_storage.AliyunMediaStorage'

python manage.py collectstatic
同步静态文件

配置生产与测试 mysql 环境

创建 不同环境 配置文件
创建Django项目 - 图1

  1. import logging
  2. logger = logging.getLogger(__name__)
  3. # Detect environment
  4. environment = os.environ.get('TT_AUDIO_ENV')
  5. if environment == 'LOCALE':
  6. logger.info('Load locale environment settings')
  7. from .env_conf.locale import *
  8. elif environment == 'PRODUCT':
  9. logger.info('Load product environment settings')
  10. from .env_conf.product import *

安装 mysql 与 mysqlclient
brew install mysql
pip install mysqlclient

启动 mysql
mysql.server start
进入 mysql 创建 数据库
mysql -uroot -p
CREATE DATABASE [数据库名];

配置本地 locale.py 数据库设置

  1. DEBUG = True
  2. DATABASES = {
  3. 'default': {
  4. 'ENGINE': 'django.db.backends.mysql',
  5. 'NAME': '数据库名',
  6. 'USER': 'root',
  7. 'PASSWORD': '密码',
  8. 'HOST': '127.0.0.1',
  9. 'PORT': '3306',
  10. 'OPTIONS': {
  11. 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
  12. 'charset': 'utf8mb4',
  13. },
  14. },
  15. }

然后删除 settings.py 的数据库配置
执行 migrate
python manage.py migrate

创建管理员账号
python manage.py createsuperuser