流程總覽
今天給大家分享一下Django的技術(shù),如上是今天分享的內(nèi)容,一起相互學(xué)習(xí)吧!
安裝Django
pip install -U django
python -m django --version
如上,安裝并更新django,查看版本為3.0.5
創(chuàng)建工程
cd G:360MoveDataUsersAdministratorDesktopDjangoProject
django-admin startproject project
tree . /F
如上圖,進(jìn)入到指定目錄,創(chuàng)建工程,并查看工程的目錄結(jié)構(gòu)。
配置數(shù)據(jù)庫
查看官方文檔
查看官方文檔:https://docs.djangoproject.com/en/3.0/ref/databases/#mysql-notes
主要有以下注意點(diǎn):
1、Django supports MySQL 5.6 and higher.
2、Django requires mysqlclient 1.3.13 or later.
3、mysqlclient is a native driver. It’s the recommended choice.
4、You can create your database using the command-line tools and this SQL:
CREATE DATABASE <dbname> CHARACTER SET utf8;
5、Connecting to the database:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8
根據(jù)官方文檔安裝mysql
現(xiàn)在,動(dòng)手實(shí)操一下:
先在centos7上安裝最高版本的mysql 8(通過下載mysql的repo源的方式):
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
yum repolist all | grep mysql
vi /etc/yum.repos.d/mysql-community.repo
查看將要安裝的版本:
yum repolist enabled | grep mysql
安裝:
yum install -y mysql-community-server
啟動(dòng):
systemctl start mysqld
開機(jī)自啟動(dòng):
systemctl enable mysqld
systemctl daemon-reload
查看密碼:
cat /var/log/mysqld.log
pwd如下圖:nyKcqlrd=6+q
登陸(輸入上面的隨機(jī)密碼):
mysql -uroot -pnyKcqlrd=6+q
更改密碼策略:
set global validate_password.policy=0;
set global validate_password.length=1;
修改mysql的root密碼(設(shè)置為yeqinfang):
alter user 'root'@'localhost' identified by 'yeqinfang';
更改默認(rèn)端口的文件:
vi /etc/my.cnf
重啟生效:
systemctl restart mysqld
查看端口:
show global variables like 'port';
創(chuàng)建數(shù)據(jù)庫:
CREATE DATABASE dbtest CHARACTER SET utf8;
關(guān)閉防火墻:
systemctl disable firewalld
systemctl stop firewalld.service
systemctl status firewalld
允許root在其他地方訪問
use mysql;
update user set Host='%' where User='root';
django配置數(shù)據(jù)庫
在配置文件進(jìn)行如下配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 將sqlite3更改
'NAME': 'dbtest', # 將os.path.join(BASE_DIR, 'db.sqlite3')更改為數(shù)據(jù)庫名
'USER': 'root',
'PASSWORD': 'yeqinfang',
'HOST': '192.168.31.101',
'PORT': '3306',
}
}
創(chuàng)建應(yīng)用
1、打開cmd,cd到manage.py同級目錄下,執(zhí)行:
python manage.py startapp myAPP
根據(jù)提示,繼續(xù)安裝客戶端:
pip install -U mysqlclient
再去創(chuàng)建應(yīng)用,接著運(yùn)行如下命令:
python manage.py startapp blog
激活應(yīng)用
在settings.py文件中,將myAPP應(yīng)用,加入到 INSTALLED_APPS選項(xiàng)中,直接在最后一行添加 myAPP,如下:
定義模型
一個(gè)表對應(yīng)一個(gè)模型,在models.py中添加模型:
class Grades(models.Model):
gname = models.CharField(max_length=20)
gdate = models.DateTimeField()
ggirlnum = models.IntegerField()
gboynum = models.IntegerField()
isDelete = models.BooleanField(default=False)
class Students(models.Model):
sname = models.CharField(max_length=20)
sgender = models.BooleanField(default=True)
sage = models.IntegerField()
scontend = models.CharField(max_length=20)
isDelete = models.BooleanField(default=False)
#關(guān)聯(lián)外鍵
sgrade = models.ForeignKey('Grades',on_delete=models.CASCADE)
在數(shù)據(jù)庫中生成數(shù)據(jù)表
1、生成遷移文件:
在manage.py目錄下執(zhí)行
python manage.py makemigrations
2、執(zhí)行遷移
python manage.py migrate
測試數(shù)據(jù)庫
在manage.py的目錄下執(zhí)行:
python manage.py shell
引入包:
from myAPP.models import Grades,Students
from django.utils import timezone
from datetime import *
Grades.objects.all()
創(chuàng)建一條數(shù)據(jù):
grade1 = Grades()
grade1.gname = 'python007'
grade1.gdate = datetime(year=2017,month=7,day=17)
grade1.ggirlnum = 3
grade1.gboynum = 70
grade1.save()
啟動(dòng)服務(wù)
python manage.py runserver 127.0.0.1:8000
登陸查看
創(chuàng)建管理員用戶(root/yeqinfang):
python manage.py createsuperuser
打開頁面(127.0.0.1:8000/admin)如下
輸入賬號密碼:root/yeqinfang
如上,我們已經(jīng)起了服務(wù),并且能夠登陸,現(xiàn)在,我們?nèi)ヅ渲米约旱捻撁妫?/p>
修改admin.py文件,添加:
from .models import Grades,Students
#注冊
admin.site.register(Grades)
admin.site.register(Students)
接著重啟服務(wù),刷新頁面
咋們的應(yīng)用已經(jīng)添加成功,我們查看我們的數(shù)據(jù):
點(diǎn)擊上圖的Grades:
點(diǎn)擊上圖的grades object
如圖,我們已經(jīng)能夠顯示原先添加的一些數(shù)據(jù)。至此,我們的流程已經(jīng)能夠跑通。后面我們再學(xué)習(xí)MTV模型的編程。