1,創(chuàng)建項目test03
進入虛擬環(huán)境py3_space01。
workon py3_space01
創(chuàng)建項目test03。
django-admin startproject test03
打開test03/settings.py文件,找到DATABASES項,默認使用SQLite3數(shù)據(jù)庫
2,mysql數(shù)據(jù)庫創(chuàng)建及配置
修改為使用MySQL數(shù)據(jù)庫,代碼如下:
將引擎改為mysql,提供連接的主機HOST、端口PORT、數(shù)據(jù)庫名NAME、用戶名USER、密碼PASSWORD。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test03', #數(shù)據(jù)庫名字,
'USER': 'root', #數(shù)據(jù)庫登錄用戶名
'PASSWORD': 'xiaoke', #數(shù)據(jù)庫登錄密碼,我自己修改了
'HOST': 'localhost', #數(shù)據(jù)庫所在主機(公司中寫真實主機地址)
'PORT': '3306', #數(shù)據(jù)庫端口
}
}
注意:數(shù)據(jù)庫test2 Django框架不會自動生成,需要我們自己進入mysql數(shù)據(jù)庫去創(chuàng)建。
下面是手動創(chuàng)建數(shù)據(jù)庫,打開新終端,在命令行登錄mysql,創(chuàng)建數(shù)據(jù)庫test2。
注意:設(shè)置字符集為utf8
create database test2 charset=utf8;
3,創(chuàng)建booktest應用
進入test03目錄,創(chuàng)建應用booktest
cd test03
python manage.py startapp booktest
4,注冊booktest應用
將應用booktest注冊到項目中:打開test03/settings.py文件,找到INSTALLED_APPS項,加入如下代碼:
5,定義模型類
- 模型類被定義在“應用/models.py”文件中,此例中為“booktest/models.py”文件。
- 模型類必須繼承自Model類,位于包django.db.models中。
- 對于重要數(shù)據(jù)使用邏輯刪除。
6,具體模型代碼
# 定義圖書模型類BookInfo
class BookInfo(models.Model):
# 圖書名稱,唯一
btitle = models.CharField(max_length=50, unique=True)
bpub_date = models.DateField()
bread = models.IntegerField(default=0)
bcomment = models.IntegerField(default=0)
# 邏輯刪除,默認不刪除
idDelete = models.BooleanField(default=False)
# 定義英雄模型類HeroInfo
class HeroInfo(models.Model):
# 英雄姓名,不唯一,可以有重名的英雄
hname=models.CharField(max_length=50,unique=False)
# 英雄性別,默認False為男性,也可以設(shè)為Integer類型,0或者1
hgender=models.BooleanField(default=False)
isDelete=models.BooleanField(default=False)
# 英雄的描述
hcontent=models.CharField(max_length=500)
# 圖書與英雄的關(guān)系為一對多的關(guān)系,所以屬性定義在英雄的模型類中
hbook=models.ForeignKey('BookInfo')
7,遷移
生成遷移文件。
python manage.py makemigrations
執(zhí)行遷移。
python manage.py migrate
打開數(shù)據(jù)庫的命令行,查看當前所有表如下圖:
表booktest_bookinfo結(jié)構(gòu)如:
默認值并不在數(shù)據(jù)庫層面生效,而是在django創(chuàng)建對象時生效。
表booktest_heroinfo結(jié)構(gòu)如下:
Django框架會根據(jù)關(guān)系屬性生成一個關(guān)系字段,并創(chuàng)建外鍵約束。
8,建立測試數(shù)據(jù)
- 在數(shù)據(jù)庫命令行中,復制如下語句執(zhí)行,向booktest_bookinfo表中插入測試數(shù)據(jù):
insert into booktest_bookinfo(btitle,bpub_date,bread,bcomment,isDelete) values
('射雕英雄傳','1980-5-1',12,34,0),
('天龍八部','1986-7-24',36,40,0),
('笑傲江湖','1995-12-24',20,80,0),
('雪山飛狐','1987-11-11',58,24,0);
- 再復制如下語句執(zhí)行,向booktest_heroinfo表中插入測試數(shù)據(jù):
insert into booktest_heroinfo(hname,hgender,hbook_id,hcontent,isDelete) values
('郭靖',1,1,'降龍十八掌',0),
('黃蓉',0,1,'打狗棍法',0),
('黃藥師',1,1,'彈指神通',0),
('歐陽鋒',1,1,'蛤蟆功',0),
('梅超風',0,1,'九陰白骨爪',0),
('喬峰',1,2,'降龍十八掌',0),
('段譽',1,2,'六脈神劍',0),
('虛竹',1,2,'天山六陽掌',0),
('王語嫣',0,2,'神仙姐姐',0),
('令狐沖',1,3,'獨孤九劍',0),
('任盈盈',0,3,'彈琴',0),
('岳不群',1,3,'華山劍法',0),
('東方不敗',0,3,'葵花寶典',0),
('胡斐',1,4,'胡家刀法',0),
('苗若蘭',0,4,'黃衣',0),
('程靈素',0,4,'醫(yī)術(shù)',0),
('袁紫衣',0,4,'六合拳',0);
9,定義視圖
- 打開booktest/views.py文件,定義視圖代碼如下:
from datetime import date
from django.shortcuts import render,redirect
from booktest.models import BookInfo
# 查詢所有圖書并顯示的視圖函數(shù)
def index(request):
books=BookInfo.objects.all()
return render(request,'booktest/index.html',{'books':books})
# 新增圖書視圖視圖函數(shù)
def addBook(request):
book=BookInfo()
book.btitle='曉可自傳'
book.bpub_date=date(2017,6,27)
book.save()
# return HttpResponse('ok')
# 重定向跳轉(zhuǎn)到首頁
return redirect('/index/')
# 根據(jù)圖書id刪除一本書的視圖函數(shù)
def delBook(request,bid):
# 查詢出圖書
b=BookInfo.objects.get(id=int(bid))
b.delete()
return redirect('/index/')
10,配置url
打開test03/urls.py文件,配置url如下:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
# 配置成功之后去booktest的urls文件中找對應的視圖函數(shù)
url(r'^',include('booktest.urls'))
]
在booktest應用下創(chuàng)建urls.py文件,代碼如下:
from . import views
from django.conf.urls import url
urlpatterns=[
url(r'^index/$',views.index),
url(r'^addBook/$',views.addBook),
url(r'^delBook/$',views.delBook)
]
11,創(chuàng)建模板
- 打開test03/settings.py文件,配置模板查找目錄TEMPLATES的DIRS。
'DIRS': [os.path.join(BASE_DIR,'templates')],
- 模板代碼如下:
<html>
<head>
<title>Python-曉可的圖書網(wǎng)站</title>
</head>
<body>
<a href="/addBook/">創(chuàng)建</a>
<ul>
{%for book in books%}
<li>{{book.btitle}}--<a href="/delBook{{book.id}}/">刪除</a></li>
{%endfor%}
</ul>
</body>
</html>
12,運行
- 運行服務器
python manage.py runserver
- 運行結(jié)果如下:(截取一部分圖片)
- 查看booktest_bookinfo表信息,可以得知增加和刪除圖書信息