MVC pattern in django

前端 未结 7 581
栀梦
栀梦 2021-01-03 03:22

This issue has been tormenting me for a while already. I\'ve read about this topic but nothing seems to clear my thoughts. I understand that they call the views templates, a

相关标签:
7条回答
  • 2021-01-03 03:27

    You can read the Django FAQ. It explains the how MVC is implemented in django. Regarding controller Django has an answer as: .

    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.

    0 讨论(0)
  • 2021-01-03 03:30

    Realy guys:

    DJANGO is MVC! But word View use for Controller.

    And Django is Full MVC, but

    Model - Model View - Template Comtroller - View

    0 讨论(0)
  • 2021-01-03 03:32

    Django is not strictly speaking MVC.

    I found this discussion very enlightening: Django is not MVC

    0 讨论(0)
  • 2021-01-03 03:36

    The views.py defines the view functions for your app and your app groups related functions together.

    However what I believe you're missing here is the urls.py. The urls file is the first part of the controller.

    The URL patterns inside urls.py dictates what you're allowed to pass in to the view function or view class (depending on what approach you took, either with function based views or class based views) and then routes it to the proper view function.

    So there's tight a coupling between the views.py and the urls.py, that makes up for the entire Controller part of MVC.

    Comparing it to Rails, would be that the urls.py is a routes.rb and the actual controller class is the views.py function/cbv.

    0 讨论(0)
  • 2021-01-03 03:36

    Could anybody help me with this? Does my mindset make any sense? Am I missing something essential here and then I can't get these concepts right?

    The only well-defined parts of MVC where nearly everyone had some sort of consensus is the M; the V and C means completely different things in different web frameworks, to the point where MVC framework really only means as not having all your code in one spaghetti (ala typical classical PHP code). You just had to accept that MVC isn't a really well defined term.

    0 讨论(0)
  • 2021-01-03 03:39

    It's a mistake to think of design patterns like MVC as unbreakable rules. They really aren't: there are all sorts of ways of implementing them, which comply with the description to a greater or lesser extent.

    This is especially the case in Python, where one of the guiding principles is "practicality beats purity" - in other words, do what works.

    In any case, Django makes no claim to be an MVC framework. On the contrary, the documentation describes it as MTV: model, template, view. After all, outside the world of design patterns everyone calls "an HTML file with syntax for variables and flow control" a template, not a view.

    (That FAQ entry also offers a possible answer to your question: the controller is the framework itself. But it goes on to emphasise that it's a mistake to try to shoehorn into these definitions.)

    0 讨论(0)
提交回复
热议问题