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,175评论 25 24
  • 就像是导火线 小小的火星 引爆巨大的负能量 竟然有一瞬间觉得自己人生完蛋 再不找人倾诉要被吞没了
    艾丽猴阅读 177评论 0 0
  • 文/水滴 写给一个外表佯装开心,却极力承受着疼痛的人。 我很久没写东西了,怪我没太执意坚持,也怪我上班生活不...
    黄水娣很二阅读 233评论 0 0
  • 我与阿三只差几步,却是天人永隔。从那时候起,我便知道:律己于心,方知生命之可贵! 01 阿三是与我家隔了一户的邻居...
    梧桐兮兮阅读 332评论 0 0