MVT pattern Django

MVC

傳統的MVC模式指的是model,view,controller

  • model是用來和數據庫交互,存取數據的
  • view是數據的呈現方式
  • controller就是接受request,然后從model取得數據,再傳遞給view最終實現數據的呈現。

MVT

Django中的MVT模式指的是model,view,template

  • model中,Django有內建的orm,可以用python語言來編寫數據庫table的schema,并且存取刪除更新數據庫中的數據。
  • view 在Django 指的是哪些數據要呈現,而不是怎樣呈現,這是和傳統MVC很不同的一點。
  • template 在Django就類似傳統的MVC中的view,定義如何呈現數據
  • Django沒有明確的controller,因為Django本身就是一個controller,它得到用戶的request請求,使用url match來向合適的view發送請求,view再和model交互得到數據,處理完數據后,發送給template完成數據的呈現。

Django is a "MTV" framework – that is, “model”, “template”, and “view.”

The "view" describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.
So, in our case, a view is the Python callback function for a particular URL, because that callback function describes which data is presented.

Furthermore, it’s sensible to separate content from presentation – which is where templates come in. In Django, a view describes which data is presented, but a view normally delegates to a template, which describes how the data is presented.

Where does the controller fit in, then? In Django’s case, it’s probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.
Reference:
https://docs.djangoproject.com/en/1.11/faq/general/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 文|半瓶話 -1- 第一次六級分數:421 說實話,這個分數我挺意外的,因為當時我已經做好了心里準備,以為自己只能...
    半瓶話閱讀 1,160評論 25 24
  • 就像是導火線 小小的火星 引爆巨大的負能量 竟然有一瞬間覺得自己人生完蛋 再不找人傾訴要被吞沒了
    艾麗猴閱讀 157評論 0 0
  • 文/水滴 寫給一個外表佯裝開心,卻極力承受著疼痛的人。 我很久沒寫東西了,怪我沒太執意堅持,也怪我上班生活不...
    黃水娣很二閱讀 230評論 0 0
  • 我與阿三只差幾步,卻是天人永隔。從那時候起,我便知道:律己于心,方知生命之可貴! 01 阿三是與我家隔了一戶的鄰居...
    梧桐兮兮閱讀 327評論 0 0