小火车 2019-11-01
作者: 何全,github地址: https://github.com/××× QQ交流群: 62-01-76-50-1
通过此教程完成从零入门,能够独立编写一个简单的CMDB系统。
目前主流的方法开发方式,分为2种:mvc 和 mvvc方式。本教程为 mvc 方式,即 django负责渲染html。后面会推出 mvvc(前后端分离)的入门教程。
教程项目地址: https://github.com/×××/husky/
框架是整个或部分系统的可重?设计,表现为一组抽象构件及构件实例间交互的?法; 另一种定
义认为,框架是可被应用开发者定制的应用骨架。Django是Python下的一款著名的Web框架。
需要你有python基本代码编写能力 以及 html js 基础, 对django有一个大概了解。
yum install epel-release -y yum -y install sqlite sqlite-devel yum install python-devel mysql-devel python36-devel.x86_64 -y sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm yum install python36 python36-setuptools -y easy_install-3.6 pip python3.6 -m pip install --upgrade pip mv /usr/bin/python /tmp/ ln -s /usr/bin/python3.6 /usr/bin/python sed -i ‘s/\#\!\/usr\/bin\/python/\#\!\/usr\/bin\/python2/‘ /usr/bin/yum sed -i ‘s/\#\! \/usr\/bin\/python/\#\! \/usr\/bin\/python2/‘ /usr/libexec/urlgrabber-ext-down mkdir -p /root/.pip/ cat > /root/.pip/pip.conf <<EOF [global] trusted-host=mirrors.aliyun.com index-url=http://mirrors.aliyun.com/pypi/simple/ EOF python -V
pip3 install django==2.2.6
file-->settings-->project interpreter--> add --> ssh interpreter 设置远端 python环境
设置/usr/bin/python3.6 目录选择 <Project root>→/opt
file--> new project --> django
mkdir -p /data/docker mkdir -p /data/mysql5722 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo sudo yum makecache fast sudo yum -y install docker-ce docker version systemctl enable docker.service systemctl start docker.service sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-‘EOF‘ {"registry-mirrors": ["https://890km4uy.mirror.aliyuncs.com"],"graph": "/data/docker"} EOF sudo systemctl daemon-reload sudo systemctl restart docker
mkdir -p /data/mysql5722 mkdir -p /data/mysql5722-cnf docker run -itd --name mysql -p 3306:3306 --mount type=bind,src=/data/mysql5722,dst=/var/lib/mysql --mount type=bind,src=/data/mysql5722-cnf,dst=/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.22 --character-set-server=utf8 yum remove mariadb -y yum install mariadb -y mysql -uroot -p123456 -h 192.168.100.99 create database husky;
husky --> settings
ALLOWED_HOSTS = [‘*‘] ##允许所有地址访问 DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘HOST‘: ‘192.168.100.99‘, ‘PORT‘: ‘3306‘, ‘NAME‘: ‘husky‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘123456‘, } } # DATABASES = { # ‘default‘: { # ‘ENGINE‘: ‘django.db.backends.sqlite3‘, # ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), # } # }
vim /usr/local/lib64/python3.6/site-packages/django/db/backends/mysql/base.py 35 #if version < (1, 3, 13): 注释掉 这两行 36 # raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.‘ % Database.__version__) vim /usr/local/lib64/python3.6/site-packages/django/db/backends/mysql/operations.py 145 if query is not None: 146 query = query.encode(errors=‘replace‘) ##修改此行
pycharm : 菜单栏 tools --> 选择 run manage.py task
makemigrations 生成数据文件
migrate 根据文件,执行生成表结构
createsuperuser
设置pycharm 项目启动 地址 为 192.168.100.99
pycharm 启动django项目 (非命令行启动)
ssh://:22/usr/bin/python3.6 -u /opt/manage.py runserver 192.168.100.99:8000 Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). October 31, 2019 - 09:33:33 Django version 2.2.6, using settings ‘husky.settings‘ Starting development server at http://192.168.100.99:8000/ Quit the server with CONTROL-C.
测试登录 http://192.168.100.99:8000/admin 输入账号密码
创建 requirements.txt 文件
pip3 freeze > requirements.txt
pip3 install -r requirements.txt ## 安装所有模块,如有增加模块,需要加到这里面