一、Linux下虚拟环境的搭建
sudo pip install virtualenv
sudo pip install virtualenvwrapper
sudo mkdir ~/.virtualenvs
vi ~/.bashrc
export WORKON_HOME= $HOME /.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh( 路径不对用find)
source ~/.bashrc
find / -name virtualenvwrapper.sh
二、虚拟环境的创建
mkvirtualenv -p /usr/bin/python3.5 环境名
rmvirtualenv
workon
deactivate
pip list
三、工程搭建
pip install django== 1.11.11
django-admin startproject 工程名
settings.py--配置文件
urls.py--路由文件
wsgi.py--WSGI兼容的Web服务器入口
manage.py--项目管理文件
python manage.py runserver ip地址:端口
python manage.py startapp 子应用名
四、jinja2配置
1.安装
pip install jinja2
2.配置
from jinja2 import Environment
def environment ( ** options) :
env = Environment( ** options)
return env
TEMPLATES = [
{
'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' ,
] ,
'environment' : 'utils.jinja2_env.jinja2_environment' ,
} ,
} ,
]
五、mysql配置
1.安装
pip install pymysql
2.配置
from pymysql import install_as_MySQLdb
install_as_MySQLdb( )
DATABASES = {
'default' : {
'ENGINE' : 'django.db.backends.mysql' ,
'HOST' : '127.0.0.1' ,
'PORT' : 3306 ,
'USER' : 'root' ,
'PASSWORD' : 'pymsql' ,
'NAME' : 'drf'
}
}
create database drf default charset= utf8
3.远程连接
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = ip
grant all privileges on *.* to 'root' @'%' identified by '密码' ;
flush privileges;
select host,user from mysql.user;
sudo /etc/init.d/mysql restart
或者
sudo service mysql restart
?serverTimezone= Hongkong
六、redis数据库配置
1.安装
pip install django-redis
2.配置
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_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "session"
七、日志配置
1.配置
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.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/log.log' ) ,
'maxBytes' : 300 * 1024 * 1024 ,
'backupCount' : 10 ,
'formatter' : 'verbose'
} ,
} ,
'loggers' : {
'django' : {
'handlers' : [ 'console' , 'file' ] ,
'propagate' : True ,
'level' : 'INFO' ,
} ,
}
}
2.使用方式
import logging
logger = logging. getLogger( 'django' )
logger. debug( '测试logging模块debug' )
logger. info( '测试logging模块info' )
logger. error( '测试logging模块error' )
3.使用git的时候注意
八、静态文件配置
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os. path. join( BASE_DIR, 'static' ) ]