环境安装配置django

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了环境安装配置django相关的知识,希望对你有一定的参考价值。

环境安装

以下的环境版本
1、vagrant_2.1.5_x86_64.msi
2、VirtualBox-5.1.0-108711-Win.exe
3、centos-7.2.box

安装VirtualBox
版本:VirtualBox-5.1.0-108711-Win

1、技术分享图片

2、技术分享图片

3、技术分享图片

4、技术分享图片

5、技术分享图片

6、技术分享图片

7、技术分享图片

安装vagrant
版本:vagrant_2.1.0_x86_64

1、技术分享图片

2、技术分享图片

3、技术分享图片

4、技术分享图片

5、重启下电脑
技术分享图片

6、验证
技术分享图片

配置启动虚拟机
在F盘新建devops 文件夹,将centos-7.2.box 拷贝到F:devops 中
1、打开cmd
技术分享图片

2、初始化
F:devops>vagrant init centos-7.2.box

技术分享图片

会在F:devops中生产配置文件Vagrantfile,修改端口转发,访问主机的8000端口会调到虚拟机的8000端口
config.vm.network "forwarded_port", guest: 8000, host: 8000

3、启动:启动过程可能比较长,耐心等待
F:devops>vagrant up
技术分享图片

自动挂载
技术分享图片

4、更改配置文件Vagrantfile启动端口
config.vm.network "forwarded_port", guest: 8000, host: 8000

报错如下:
技术分享图片

解决:安装Windows6.1-KB2506143-x64.msu Windows6.1-KB2819745-x64-MultiPkg.msu

6、再次运行vagrant up

7、可以不用打开VirtualBox ,直接通过xshell连接虚拟机

可以通过key连接,key在F:devops.vagrantmachinesdefaultvirtualbox,导入这个key
技术分享图片

技术分享图片
技术分享图片

实现了共享目录

虚拟机的镜像文件存在以下位置

技术分享图片

技术分享图片

虚拟机安装环境

1、[[email protected] tools]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
2、yum install net-tools *sz openssl-devel readline-devel unzip vim -y
3、tar xf Python-3.6.6.tgz
4、cd Python-3.6.6
5、./configure --prefix=/usr/local/python36
6、make && make install
7、导入pip源
tee /etc/pip.conf <<EOF
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
[list]
format=columns
EOF

[[email protected] Python-3.6.6]# vi /etc/pip.conf

[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
[list]
format=columns

8、新建虚拟环境
用普通用户创建
安装:
[[email protected] ~]$ sudo /usr/local/python36/bin/pip3 install virtualenv

创建:
[[email protected] ~]$ /usr/local/python36/bin/virtualenv ./python36env

进入:
[[email protected] ~]$ source ./python36env/bin/activate

(python36env) [[email protected] ~]$ pip list
Package Version


pip 18.1
setuptools 40.4.3
wheel 0.32.1

安装django
(python36env) [[email protected] ~]$ pip install "django>=1.11,<2.0"

安装数据库:
(python36env) [[email protected] ~]$ sudo yum -y install mariadb mariadb-server mariadb-devel

编辑:
vi /etc/my.ini
mysqld的模块中
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = ‘SET NAMES utf8’
character-set-server = utf8

启动:
(python36env) [[email protected] ~]$ sudo systemctl start mariadb
(python36env) [[email protected] ~]$ sudo systemctl enable mariadb

安装数据库模块:
(python36env) [[email protected] ~]$ pip install mysqlclient

mysql初始化:
(python36env) [[email protected] ~]$ mysql_secure_installation

(python36env) [[email protected] ~]$ mysql_secure_installation


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we‘ll need the current
password for the root user.  If you‘ve just installed MariaDB, and
you haven‘t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: 123456
Re-enter new password: 123456
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from ‘localhost‘.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named ‘test‘ that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you‘ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

创建数据库:

(python36env) [[email protected] ~]$ mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]> create database devops CHARACTER SET utf8;
Query OK, 1 row affected (0.01 sec)

创建django项目:
(python36env) [[email protected] ~]$ cd /vagrant/
(python36env) [[email protected] vagrant]$ django-admin startproject devops

在vagrant中创建是因为这个目录关联到了window目录,这样可以在windows上开发
技术分享图片

pycharm设置:
1、在pycharm中打开F:devops
2、添加:
技术分享图片

3、设置关联linux
当前目录必须和Vagrantfile同一个目录才行

技术分享图片

技术分享图片

4、设置数据库:
在settings中设置:

DATABASES = {
    ‘default‘: {
        ‘ENGINE‘: ‘django.db.backends.mysql‘,
        ‘NAME‘: ‘devops‘,
        ‘USER‘: ‘root‘,
        ‘PASSWORD‘: ‘123456‘,
        ‘HOST‘: ‘127.0.0.1‘,
        ‘PORT‘: 3306,
        ‘OPTIONS‘:{
            ‘init_command‘: ‘SET default_storage_engine=INNODB;‘,
        },
    }
}

5、验证:
技术分享图片

创建app

1、将python3加入到环境变量中
(python36env) [[email protected] ~]$ vi .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/vagrant/python36env/bin

export PATH

2、(python36env) [[email protected] ~]$ source .bash_profile

3、(python36env) [[email protected] ~]$ cd /vagrant/devops/

4、(python36env) [[email protected] devops]$ python manage.py startapp dashboard

5、设置urls路径:
技术分享图片

6、添加app
技术分享图片

7、
技术分享图片

验证:
技术分享图片

数据格式为字典或者列表:

技术分享图片

练习个登录操作
get:获取
post:提交
delete:删除

1、安装模块
pip install requests

2、编写url
技术分享图片

3、编写view
技术分享图片

4、在settings.py中禁用csrf功能
技术分享图片

3、验证:
技术分享图片

同步数据库
1、使用django命令行工具同步数据库
python manage.py makemigrations
python manage.py migrate
python manage.py showmigrations

技术分享图片

技术分享图片

创建用户
1、创建普通用户
技术分享图片

2、查看数据库
技术分享图片

3、创建管理员用户
技术分享图片

4、查看,1表示管理员,0表示普通用户
技术分享图片

5、修改密码
技术分享图片

以上是关于环境安装配置django的主要内容,如果未能解决你的问题,请参考以下文章

Django开发环境配置(win10)

【踩坑】linux安装配置django环境踩坑记

环境安装配置django

linux安装配置python环境以及虚拟环境和django下载

Django的安装及配置环境变量

Django 的安装,配置环境,启动项目。