Django學習之二 專案和應用

2021-09-24 12:05:16 字數 2940 閱讀 4551

在django中,像上一節那樣的一套目錄結構和其中的設定就是乙個django可識別的專案。

應用指的就是一組model(資料模型)、views(檢視)、templates(模板)和urls的集合。

django框架通過使用應用,為站點提供各種功能,應用還可以被復用在不同的專案中。你可以將乙個專案理解為乙個站點,站點中包含很多功能,比如部落格,wiki,論壇,每一種功能都可以看作是乙個應用。

資料模型 是繼承了 django.db.models.model 的類

django為建立和運算元據模型提供了orm,為在models.py 檔案中定義的每乙個類,在資料庫中建立對應的資料表。

from django.db import models

from django.utils import timezone

from django.contrib.auth.models import user

class post(models.model):

status_choices = (('draft', 'draft'), ('published', 'published'))

title = models.charfield(max_length=250)

slug = models.slugfield(max_length=250, unique_for_date='publish')

author = models.foreignkey(user, on_delete=models.cascade, related_name='blog_posts')

body = models.textfield()

publish = models.datetimefield(default=timezone.now)

created = models.datetimefield(auto_now_add=true)

updated = models.datetimefield(auto_now=true)

status = models.charfield(max_length=10, choices=status_choices, default='draft')

class meta:

ordering = ('-publish',)

def __str__(self):

return self.title

資料模型的字段定義可參考

]進入專案的根目錄,執行 python manage.py makemigrations blog

d:\work\code\python\django_01>python manage.py makemigrations blog

migrations for 'blog':

blog\migrations\0001_initial.py

- create model post

該命令執行後會在blog 應用下的migrations 目錄裡新增乙個0001_initial.py 檔案,可以開啟該檔案看一下遷移資料是什麼樣子的。乙個遷移資料檔案裡包含了與其他遷移資料的依賴關係,以及實際要對資料庫執行的操作。

為了了解django實際執行的sql語句,可以使用sqlmigrate 加上遷移檔名,會列出要執行的sql語句,但不會實際執行 python manage.py sqlmigrate blog 0001

d:\work\code\python\django_01>python manage.py sqlmigrate blog 0001

begin;

---- create model post

--create table "blog_post" ("id" integer not null primary key autoincrement, "title" varchar(250) not null, "slug" varchar(250) not null, "body" text not null, "publish" datetime not null, "created" datetime not null, "updated" datetime not null, "status" varchar(10) not null, "author_id" integer not null references "auth_user" ("id") deferrable initially deferred);

create index "blog_post_slug_b95473f2" on "blog_post" ("slug");

create index "blog_post_author_id_dd7a8485" on "blog_post" ("author_id");

commit;

具體的sql細節可能會與上面的有變化,上邊的輸出針對sqlite資料庫。可以看到表名被設定為應用名加上小寫的類名( blog_post )也可以通過在meta 類中使用db_table屬性設定表名。django自動為每個模型建立了主鍵,也可以通過設定某個模型字段引數primary_key=true 來指定主鍵。預設的主鍵列名叫做id ,和這個列同名的id 欄位會自動新增到你的資料模型上。(即post 類被django新增了post.id 屬性)。

讓資料庫與新的資料模型進行同步,在命令列中輸入下列命令:

d:\work\code\python\django_01>python manage.py migrate如果之後又編輯了models.py 檔案,對已經存在的資料模型進行了增刪改,或者又新增了新的資料模型,必須重新執行

makemigrations 建立新的資料遷移檔案然後執行migrate 命令同步資料庫。

Django學習之路3 專案與應用

通過 djaong 編寫的每個應用都是由 python 包組成的,這些包存放在你的 python path 中並且遵循一定的命名規範。django 提供了個實用工具可以自動生成乙個應用的基本目錄架構,因此你可以專注於編寫 而不是去建立目錄。專案與應用之間有什麼不同之處?應用是乙個提供功能的 web ...

軟體專案管理之二 專案跟蹤

上次談了專案管理之專案進度計畫,今天我們來談一下關於專案跟蹤的問題,專案跟蹤是為了保持對專案的控制,了解專案的動態,以使專案向好的方面發展。我們有些專案負責人經常反映專案的開發往往有悖於專案之初制定的專案進度計畫,專案進度計畫總是得不到很好的貫徹執行,最後專案經常會出現延誤的狀況。其實出現這樣的情況...

Django 基礎(二) 專案搭建指南

在python的終端視窗下執行以下命令,如圖 提前安裝 mysqlclient 擴充套件庫,開啟 cmd 視窗執行 或通過whl檔案安裝 pip install mysqlclient開啟全域性 settings.py 檔案,修改以下兩部分 配置資料庫 databases django.contri...