The first step: engineering structures
1. Create a remote warehouse, and cloning a local repository
2. Create a virtual environment locally
3. In a virtual environment, install django Version: 1.11.11 (stable)
4. Create a project using the django
Step two: modify directory
1. Add a directory, configuration files moved into in, and then changed its name to dev.py
2. Modify access to the configuration file path: manage.py be modified in
The third step: Configuring the development environment
1. Add jinja2 in a virtual environment
2. Configure Jinja2 template engine
TEMPLATES = [
{
# 修改为 jinja2 模板引擎
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'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',
],
},
},
]
3.Jinja2 create a template engine environment configuration file
to create a utils package under the project, add the files under the utils jinja2_env.py
4. Create a template engine written Jinja2 environment configuration code
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
from jinja2 import Environment
def jinja2_environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
"""
确保可以使用Django模板引擎中的{% url('') %} {% static('') %}这类的语句
"""
5. After the addition was complete, the configuration file, the registered settings.py
TEMPLATES = [
{
# jinja2 模板引擎
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'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',
],
# 添加 Jinja2 模板引擎环境
'environment': 'meiduo_mall.utils.jinja2_env.jinja2_environment',
},
},
]
Step Four: Configure Database
1. Create a new Mysql database
2. Configure Mysql (configuration dev.py years)
3. Remember to install PyMySQL Expansion Pack (PIP install pymysql)
4. __init__.py file in a subdirectory of the project of the same name, add:
# 告知django使用pymysql:
from pymysql import install_as_MySQLdb
# 调用该函数:
install_as_MySQLdb()
5. Configure Redis, mounting extended package django-redis, and add the dev.py:
CACHES = {
"default": { # 默认存储信息: 存到 0 号库
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
"session": { # session 信息: 存到 1 号库
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "session"
Step Five: Configure log
1.dev.py file:
LOGGING = {
'version': 1,
'disable_existing_loggers': False, # 是否禁用已经存在的日志器
'formatters': { # 日志信息显示的格式
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
},
},
'filters': { # 对日志进行过滤
'require_debug_true': { # django在debug模式下才输出日志
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': { # 日志处理方法
'console': { # 向终端中输出日志
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'file': { # 向文件中输出日志
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(os.path.dirname(BASE_DIR), 'logs/meiduo.log'), # 日志文件的位置
'maxBytes': 300 * 1024 * 1024,
'backupCount': 10,
'formatter': 'verbose'
},
},
'loggers': { # 日志器
'django': { # 定义了一个名为django的日志器
'handlers': ['console', 'file'], # 可以同时向终端与文件中输出日志
'propagate': True, # 是否继续传递日志信息
'level': 'INFO', # 日志器接收的最低日志级别
},
}
}
Create a directory xxx.log under 2.logs
Step 6: Configuring static files
Or dev.py file:
# 默认有部分:
STATIC_URL = '/static/'
# 要配置静态文件加载路径
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
to sum up:
More disgusting is the third package of installation, because national circumstances, install third-party package is very slow.
It is recommended Baidu: python replace pip mirroring
finished modifying the source image and then tinker much more comfortable