ubuntu-从零开始配置django

  1. 更换阿里源
    访问阿里云官方镜像站
    https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.3e221b11E7fcmp
    复制所需的配置(18.04):
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
	
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
	
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
	
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
	
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  1. 打开并替换配置文件中的内容
vim /etc/apt/sources.list

保存后更新

apt update
apt upgrade
  1. 安装pip
apt install python-pip
  1. 安装virtualenv
pip install virtualenv
pip install virtualenvwrapper
# win环境
pip install virtualenvwrapper-win
#.\venv\Scripts\activate : 无法加载文件 venv\Scripts\activate.ps1,因为在此系统上禁止运行脚本 
#解决办法
以管理员身份运行windows powershell
进入到虚拟环境的文件夹比如我的存放C:/Users/user1/env/xxx_project_env,cd C:/Users/user1/env/xxx_project_env
Scripts\activate.bat
Set-ExecutionPolicy RemoteSigned,此时显示如下:
 执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 http://go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y](Y)  [A] 全是(A)  [N](N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”):

  1. 创建虚拟环境(管理指令)
mkvirtualenv  Django -p  python3
进入虚拟环境
workon xxx
退出虚拟环境
deactivate
删除虚拟环境
rmvirtualenv xxx
列出虚拟环境列表
lsvirtualenv
  1. 安装Django
pip  install  django==2.2.5
  1. 创建工程
django-admin  startproject  meiduo_mall
  1. 配置mysql
1.新建 MySQL 数据库:meiduo

create database meiduo charset=utf8;
2.新建 MySQL 用户

create user itcast identified by '123456';
3.授权 itcast 用户访问 meiduo 数据库

grant all on meiduo_mall.* to 'meiduo'@'%';
4 授权结束后刷新特权

flush privileges;
在 dev.py 中:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # 数据库引擎
        'HOST': '127.0.0.1', # 数据库主机
        'PORT': 3306, # 数据库端口
        'USER': 'root', # 数据库用户名
        'PASSWORD': '123456', # 数据库用户密码
        'NAME': 'meiduo' # 数据库名字
    },
}
  1. 安装mysqlclient
1.先安装 客户端程序:
sudo apt install -y mysql-client
 1. 基于python安装mysqlclient需要依次安装以下库:
sudo apt-get install libmysqlclient-dev
sudo apt install libssl-dev
sudo apt install libcrypto++-dev
sudo pip3 install mysqlclient
  1. 配置 Redis 数据库
在 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"
  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在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(BASE_DIR, 'logs/meiduo.log'),  # 日志文件的位置
            'maxBytes': 300 * 1024 * 1024,
            'backupCount': 10,
            'formatter': 'verbose'
        },
    },
    'loggers': {  # 日志器
        'django': {  # 定义了一个名为django的日志器
            'handlers': ['console', 'file'],  # 可以同时向终端与文件中输出日志
            'propagate': True,  # 是否继续传递日志信息
            'level': 'INFO',  # 日志器接收的最低日志级别
        },
    }
}
  1. 日志记录器的使用
# 1. 导入: 在需要使用的位置导入
import logging

# 2. 创建日志记录器: 导入后创建日志器才能使用
logger = logging.getLogger('django')

# 3. 根据不同情况, 输出日志
logger.debug('调试信息')
logger.info('打印信息')
logger.error('错误信息')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值