1.進(jìn)入Python虛擬環(huán)境
source xxx/bin/activate
2.進(jìn)入當(dāng)前用戶有操作權(quán)限的目錄:
django-admin startproject First_Django
3.查看新建的項(xiàng)目內(nèi)部文件
//進(jìn)入First_Django項(xiàng)目
cd First_Django
//查看項(xiàng)目的子目錄文件
ls
> First_Django(此子目錄名稱與項(xiàng)目名稱相同弛秋,作用是配置項(xiàng)目) manage.py (此文件用來管理項(xiàng)目)
// 進(jìn)入First_Django項(xiàng)目的First_Django子目錄
cd First_Django
// 查看First_Django項(xiàng)目的First_Django子目錄文件
> __init__.py settings.py urls.py wsgi.py
整個(gè)項(xiàng)目文件結(jié)構(gòu):
|____First_Django
| |____First_Django
| | |______init__.py
| | |____settings.py
| | |____urls.py
| | |____wsgi.py
| |____manage.py
4.創(chuàng)建應(yīng)用:
一個(gè)項(xiàng)目里可以創(chuàng)建一個(gè)或多個(gè)應(yīng)用
// 在項(xiàng)目的目錄下
Python manage.py startapp FirstApp
// 查看項(xiàng)目
ls
> First_Django booktest(新建的項(xiàng)目) manage.py
查看booktest文件結(jié)構(gòu):
tree booktest/
>
booktest/
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
5.編寫代碼
// models.py 中編寫模型代碼
class Bookinfo(models.Model):
btitle = models.CharField(max_length=20)
bpub_date = models.DateTiemField()
class HeroInfo(models.Model):
hname=models.CharField(max_length=10)
hgender=models.BooleanField()
hcontent=models.CharFeild(max_length=1000)
hbook=models.ForeignKey(Bookinfo)
6.跑起服務(wù)器
Python manage.py runserver
如果報(bào)錯(cuò)??的錯(cuò)誤:
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
注冊遷移:
在setting.py文件中的INSTALLED_APPS字段中添加項(xiàng)目名稱:
執(zhí)行遷移:
python manage.py migrate
啟動(dòng)服務(wù)器:
Python manage.py runserver
執(zhí)行python manage.py migrate 如果報(bào)錯(cuò)??的錯(cuò)誤:
(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
依次執(zhí)行一下命令:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
運(yùn)行效果:
(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$ python manage.py makemigrations
Migrations for 'booktest':
booktest/migrations/0001_initial.py
- Create model BookInfo
- Create model HeroInfo
(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, booktest, contenttypes, sessions
Running migrations:
Applying booktest.0001_initial... OK
(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
July 16, 2017 - 06:06:04
Django version 1.11.3, using settings 'First_Django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
7.測試
python manage.py shell
//導(dǎo)入類
>>> from booktest.models import *
>>> b = BookInfo()
>>> b.btitle = 'abc'
//導(dǎo)入python時(shí)間
>>> from datetime import datetime
>>> b.bpub_date = datetime(year=1990,month=1,day=12)
>>> b.save() //執(zhí)行了數(shù)據(jù)庫的insert操作
//打印對象信息
>>> BookInfo.objects.all() //執(zhí)行了數(shù)據(jù)庫的select操作
<QuerySet [<BookInfo: BookInfo object>]>
>>> b = BookInfo.objects.last() //執(zhí)行了數(shù)據(jù)庫的select操作
>>> b.btitle = 'python'
>>> b.save() //執(zhí)行了數(shù)據(jù)庫的update操作
>>> b.delete() //執(zhí)行了數(shù)據(jù)庫的delete操作