标签:tab ant ini source error evel mys exp course
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‘, ], # 将模板标签内置到模板中 https://docs.djangoproject.com/en/2.1/topics/templates/ ‘builtins‘: [‘django.templatetags.static‘], }, }, ]
创建数据库
mysql> create database tzproject charset=utf8mb4;
Query OK, 1 row affected (0.00 sec)
mysql> create user ‘dj_user‘@‘%‘ identified by ‘pythonvip‘; Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on tzproject.* to ‘dj_user‘@‘%‘; Query OK, 0 rows affected (0.03 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) # 授权创建一起写 grant all privileges on tzproject.* to ‘xinlan‘@‘%‘ identified by ‘pythonvip‘
方法一:直接在settings.py文件中添加数据库配置信息
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, # 数据库引擎 ‘NAME‘: ‘tzproject‘, # 数据库名 ‘USER‘: ‘dj_user‘, # 用户名 ‘PASSWORD‘: ‘pythonvip‘, # 密码 ‘HOST‘: ‘127.0.0.1‘, # 主机IP ‘PORT‘: 3306 # 端口 } }
db.cnf文件内容:
[client] database = tzproject user = dj_user password = pythonvip host = 127.0.0.1 port = 3306 default-character-set = utf8
在settings.py文件中引入db.cnf
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘OPTIONS‘:{ ‘read_default_file‘: ‘utils/db/db.cnf‘, } } }
1. 安装mysqlclient
# 首先更新ubuntu sudo apt-get update # 然后按照依赖库 sudo apt-get install default-libmysqlclient-dev # 再切换到虚拟环境中 workon tzproject # 最后安装mysqlclient pip install mysqlclient -i https://pypi.doubanio.com/simple
2.安装pymysql
# 安装pymysql pip install pymysql -i https://pypi.doubanio.com/simple
推荐使用第一种方式,mysqlclient
更快更安全,官方推荐。
用于存放用户session信息,以及需要缓存的各种信息。
# 在虚拟机中安装django-redis pip install django-redis
# settings.py文件中指定redis配置 CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/0", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, ‘session‘: { ‘BACKEND‘: ‘django_redis.cache.RedisCache‘, ‘LOCATION‘: ‘redis://127.0.0.1:6379/1‘, ‘OPTIONS‘: { ‘CLIENT_CLASS‘: ‘django_redis.client.DefaultClient‘, } }, } # session存储缓存设置 SESSION_ENGINE = ‘django.contrib.sessions.backends.cache‘ SESSION_CACHE_ALIAS = ‘session‘
文档地址:https://django-redis-chs.readthedocs.io/zh_CN/latest/
django文档:https://docs.djangoproject.com/en/2.2/topics/http/sessions/#using-cached-sessions
用于记录系统运行过程中的各种日志信息。
在项目根目录中创建一个logs文件夹,用于存放日志文件
# 日志 LOGGING = { # 版本 ‘version‘: 1, # 是否禁用已存在的日志器 ‘disable_existing_loggers‘: False, ‘formatters‘: { ‘verbose‘: { ‘format‘: ‘{levelname} {asctime} {module} {lineno:d} {message}‘, ‘style‘: ‘{‘, }, ‘simple‘: { ‘format‘: ‘{levelname} {module} {lineno:d} {message}‘, ‘style‘: ‘{‘, }, }, ‘filters‘: { ‘require_debug_true‘: { ‘()‘: ‘django.utils.log.RequireDebugTrue‘, }, }, ‘handlers‘: { ‘console‘: { ‘level‘: ‘DEBUG‘, ‘filters‘: [‘require_debug_true‘], ‘class‘: ‘logging.StreamHandler‘, ‘formatter‘: ‘simple‘ }, ‘file‘: { ‘level‘: ‘INFO‘, # 这个handler可以记录一组日志文件 ‘class‘: ‘logging.handlers.RotatingFileHandler‘, ‘filename‘: os.path.join(BASE_DIR, ‘logs/tz_django.log‘), # 单个日志文件最大字节数 ‘maxBytes‘: 300*1024*1024, # 日志文件个数 ‘backupCount‘: 10, ‘formatter‘: ‘verbose‘ }, }, ‘loggers‘: { ‘django‘: { ‘handlers‘: [‘console‘, ‘file‘], ‘level‘: ‘INFO‘, # 日志器接收的最低级别 ‘propagate‘: True, }, }, }
使用方法
# 使用方法 # import the logging library import logging # Get an instance of a logger # 此处的django为,settings.py文件中,LOGGING配置下的loggers中定义的日志器名称 logger = logging.getLogger(‘django‘) def my_view(request, arg1, arg): ... if ***: # Log an error message logger.error(‘Something went wrong!‘)
官方文档:https://docs.djangoproject.com/en/2.2/topics/logging/#module-django.utils.log
# 修改语言 LANGUAGE_CODE = ‘zh-hans‘ # 修改时区 TIME_ZONE = ‘Asia/Shanghai‘ USE_I18N = True USE_L10N = True # 使用时区 USE_TZ = True
STATIC_URL = ‘/static/‘ STATICFILES_DIRS = [ os.path.join(BASE_DIR, ‘static‘) ]
然后cd到apps目录创建各子应用
~$ cd ~/code/tztz/apps/ ~/code/tztz/apps$ workon tzproject (tzproject) ~/code/tztz/apps$ python ../manage.py startapp news (tzproject) ~/code/tztz/apps$ python ../manage.py startapp course (tzproject) ~/code/tztz/apps$ python ../manage.py startapp doc (tzproject) ~/code/tztz/apps$ python ../manage.py startapp user
再到settings文件中,加入如下设置
import sys # 创建应用之后,把apps目录加入到sys.path中 sys.path.insert(0, BASE_DIR) sys.path.insert(1, os.path.join(BASE_DIR, ‘apps‘))
接着分别在apps文件夹和项目根目录文件夹上右键,加入到Source Root中,优化导入路径(pycharm提示)。
最后将应用添加到INSTALLED_APPS列表中
INSTALLED_APPS = [ # ... ‘user‘, ‘news‘, ‘doc‘, ‘course‘ ]
Django项目: 项目环境搭建 ---- 二、django项目设置
标签:tab ant ini source error evel mys exp course
原文地址:https://www.cnblogs.com/Tmclri/p/11515365.html