美文网首页
第二-四章 mysql配置 Django高级实战 开发企业级问答

第二-四章 mysql配置 Django高级实战 开发企业级问答

作者: 吕阳 | 来源:发表于2019-05-18 20:37 被阅读0次

    2-1 Pipenv管理项目环境(附文档)

    pip3 install pipenv
    pip3 --where
    pip3 --venv
    pip3 --py
    
    • -bash: pipenv: command not found
    ln -sv /usr/local/python3/bin/pipenv /usr/bin/pipenv
    

    2-2 Pipenv管理Python虚拟环境
    2-3 优先使用自定义的用户模型
    2-4 使用通用类视图(GCBV)
    2-5 在系统环境变量中保存敏感信息
    2-6 为不同环境分别配置settings.py文件
    2-7 一定要编写测试用例

    第4章 Cookiecutter火速搭建项目+开发环境介绍

    4-1 Cookiecutter安装和使用(附文档)

    • pip 先安装cookiecutter
    • 确认可以ssh clone github上的代码.
    /usr/local/python3/bin/cookiecutter git@github.com:pydanny/cookiecutter-django.git
    

    然后用django模板.

    4-2 使用Cookiecutter快速搭建Django应用
    4-3 cookiecutter-django火速搭建项目

    [root@localhost test]# /usr/local/python3/bin/cookiecutter git@github.com:pydanny/cookiecutter-django.git
    You've downloaded /root/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-download it? [yes]: no
    Do you want to re-use the existing version? [yes]: yes
    project_name [My Awesome Project]: zanhu
    project_slug [zanhu]: zanhu
    description [Behold My Awesome Project!]: a Q&A website
    author_name [Daniel Roy Greenfeld]: lvyang
    domain_name [example.com]: zanhu.com
    email [lvyang@example.com]: tylvyang@qq.com
    version [0.1.0]: 
    Select open_source_license:
    1 - MIT
    2 - BSD
    3 - GPLv3
    4 - Apache Software License 2.0
    5 - Not open source
    Choose from 1, 2, 3, 4, 5 (1, 2, 3, 4, 5) [1]: 5
    timezone [UTC]: Asia/Shanghai
    windows [n]: n
    use_pycharm [n]: y
    use_docker [n]: n
    Select postgresql_version:
    1 - 11.3
    2 - 10.8
    3 - 9.6
    4 - 9.5
    5 - 9.4
    Choose from 1, 2, 3, 4, 5 (1, 2, 3, 4, 5) [1]: 1
    Select js_task_runner:
    1 - None
    2 - Gulp
    Choose from 1, 2 (1, 2) [1]: 1
    Select cloud_provider:
    1 - AWS
    2 - GCP
    3 - None
    Choose from 1, 2, 3 (1, 2, 3) [1]: 3
    custom_bootstrap_compilation [n]: n
    use_compressor [n]: y
    use_celery [n]: y
    use_mailhog [n]: n
    use_sentry [n]: n
    use_whitenoise [n]: n
    use_heroku [n]: n
    use_travisci [n]: n
    keep_local_envs_in_vcs [y]: n
    debug [n]: y
     [WARNING]: You chose not to use a cloud provider, media files won't be served in production.
     [SUCCESS]: Project initialized, keep up the good work!
    

    4-4 项目开发环境介绍(服务器环境和Pycharm远程开发设置)
    systemctl status redis

    
    直接yum 安装的redis 不是最新版本
    
    yum install redis
    如果要安装最新的redis,需要安装Remi的软件源,官网地址:http://rpms.famillecollet.com/
    
    yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    然后可以使用下面的命令安装最新版本的redis:
    
    yum --enablerepo=remi install redis
    安装完毕后,即可使用下面的命令启动redis服务
    
    service redis start
    或者
    systemctl start redis
    
    mysql> set password for root@localhost = password('123');  
    
    yum 
    

    4-5 删改部分文件完成项目初始模板

    介绍神器Cookiecutter,它的优点和局限,按照确定的技术栈选择cookiecutter-django搭建具有高完成度的项目,学习Python项目如何组织代码,如何管理配置,如何管理依赖。项目开发环境介绍:Pycharm连接CentOS 7远程开发设置,以及MySQL用户权限管理和安全设置。

    [精华]4-4 项目开发环境介绍(服务器环境和Pycharm远程开发设置)

    需要安装mysql和redis

    centos配置

    • yum install python
      -devel mysql-devel mysql-lib

    django-copress依赖

    • yum install bzip2-devel

    • 数据库操作
      登录:

    mysql -uroot -p  
    
    • 显示数据库
    show databases;
    
    • 创建数据库
    create database zanhu charset utf8;
    
    • 创建用户
    create user 'zanhu'@'%' identified by 'zAnhu6.6';
    
    • 用户授权zanhu
    grant  all on zanhu.* to 'zanhu'@'%';
    
    • 用户授权test_zanhu
    grant  all on test_zanhu.* to 'zanhu'@'%';
    
    • 更新权限信息
    flush privileges;
    
    • 退出
    exit
    

    配置pycharm 远程开发环境.

    1. 打开空的zanhu文件夹.
    2. Tools--Deployment--Configuration
    1121568639426_.pic_hd.jpg 1131568639446_.pic.jpg

    2 依赖环境. 虚拟环境依赖远程

    1161568640437_.pic_hd.jpg 1141568640290_.pic_hd.jpg
    • 同步代码下载linux到本地.


      1181568640820_.pic.jpg
      1. 运行支持. 配置pycharm 服务器.


        1201568641245_.pic.jpg
    1191568641160_.pic_hd.jpg
    • 总结 1. 同步代码. 2 依赖环境. 3. 运行支持.

    依赖服务器的python 解释器

    • create database zanhu charset utf8;

    • create user 'zanhu02'@'%' identified by 'zAnhu6.6';

    • grant all on zanhu02.* to 'zanhu02'@'%';

    • flush privileges;

    pipenv

    • pipenv --py
    • pipenv --three

    4-5 删改部分文件完成项目初始模板

    • /doc/ 删除.

    • /utillity 用不到 删除 . 安装 requirements local.txt就行.

    • pipenv install -r requirements/local.txt

    • config/setting/base.py

    • 是否读取.env文件, 开发阶段改成True.

    READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False)
    
    • 语言改成中文
    LANGUAGE_CODE = "zh-Hans"
    
    
    DATABASES['default']['ATOMIC_REQUESTS'] = True  # 将HTTP请求封装到事务
    
    
    • 'django.contrib.humanize', 开启.模板标签.
      
    
    DJANGO_APPS = [
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.humanize', 
        'django.forms',
    ]
    
    CSRF_COOKIE_HTTPONLY = False  # 默认为False, 如果设置为True, JS将不能获取到CSRF cookie
    
    • EMAIL
    # EMAIL
    # ------------------------------------------------------------------------------
    # https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
    EMAIL_BACKEND = env('DJANGO_EMAIL_BACKEND', default='django.core.mail.backends.smtp.EmailBackend')
    # https://docs.djangoproject.com/en/dev/ref/settings/#email-host
    EMAIL_HOST = env('DJANGO_EMAIL_HOST')
    # https://docs.djangoproject.com/en/dev/ref/settings/#email-port
    EMAIL_USE_SSL = env('DJANGO_EMAIL_USE_SSL', default=True)
    EMAIL_PORT = env('DJANGO_EMAIL_PORT', default=465)
    EMAIL_HOST_USER = env('DJANGO_EMAIL_HOST_USER')
    EMAIL_HOST_PASSWORD = env('DJANGO_EMAIL_HOST_PASSWORD')
    DEFAULT_FROM_EMAIL = env('DJANGO_DEFAULT_FROM_EMAIL')
    
    • local.py
    ALLOWED_HOSTS = ["*"]
    

    连接数据库.

    DATABASE_URL=mysql://yousi:zAnhu6.6@127.0.0.1:3306/yousi
    
    
    • wsgi
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
    
    

    在linux上安装psycopg2出错--Error: pg_config executable not found.

    yum install postgresql postgresql-devel python-devel
    
    pipenv run python manage.py migrate
    
    
    • 防火墙.
    systemctl stop firewalld.service 
    

    ···

    brew switch openssl 1.0.2s
    ···

    相关文章

      网友评论

          本文标题:第二-四章 mysql配置 Django高级实战 开发企业级问答

          本文链接:https://www.haomeiwen.com/subject/lvuanqtx.html