python djiago創(chuàng)建項目

1.進入Python虛擬環(huán)境

source  xxx/bin/activate

2.進入當前用戶有操作權限的目錄:

django-admin startproject First_Django

3.查看新建的項目內(nèi)部文件

//進入First_Django項目
cd  First_Django
//查看項目的子目錄文件
ls  
> First_Django(此子目錄名稱與項目名稱相同,作用是配置項目)    manage.py (此文件用來管理項目)
// 進入First_Django項目的First_Django子目錄
cd  First_Django
// 查看First_Django項目的First_Django子目錄文件
> __init__.py settings.py urls.py     wsgi.py

整個項目文件結構:
|____First_Django
| |____First_Django
| | |______init__.py
| | |____settings.py
| | |____urls.py
| | |____wsgi.py
| |____manage.py

4.創(chuàng)建應用:

一個項目里可以創(chuàng)建一個或多個應用

// 在項目的目錄下
Python manage.py startapp FirstApp

// 查看項目
ls 
> First_Django booktest(新建的項目)     manage.py

查看booktest文件結構:
 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.跑起服務器

Python manage.py runserver

如果報錯??的錯誤:

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字段中添加項目名稱:
執(zhí)行遷移:
python manage.py migrate
啟動服務器:
Python manage.py runserver

執(zhí)行python manage.py migrate 如果報錯??的錯誤:

(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

運行效果:

(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 
//導入類
>>> from booktest.models import *
>>> b = BookInfo()
>>> b.btitle = 'abc'
//導入python時間
>>> 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操作

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容