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/