6

I am very new to the world of web development and MVC architectures. I am currently working on django which i believe is an MVC framework. if i am right, for a web application MVC implies

views is the front end models is the backend controllers are the glue between frontend and backend

well if the above is true and views are the only frontend parts what exactly is the function of frontend frameworks like backbone, angular? how exactly do they deploy the mvc concept? Also when building a simple blogging site which framework would be preferrable? and also are there instances of websites working on both frontend and backend mvc frameworks? please give examples. for clarification i went through this question: In the Model-View-Controller principle, what is the Frontend and what is the Backend? but couldnt understand fully. thank you

Community
  • 1
  • 1
user2627317
  • 89
  • 1
  • 2
  • 9
  • I honestly doubt that a blogging site would require you to implement MVC or MVC-inspired architecture. The point of this pattern is to introduce additional set of constraints, that impose large scale structure for applications, where simple adherence to OOP principles and practices is not enough anymore. In perfect world you would apply MVC on top of existing codebase when OOP alone cannot contain the complexity. – tereško Aug 02 '13 at 18:09
  • by blogging site i dont mean my personal blogging site.. something like tumblr or blogger which can feature others blog... shudn't we implement mvc in it? – user2627317 Aug 02 '13 at 19:13

3 Answers3

11

There is no one-on-one analogy of front- and backend and the MVC model. For example, the admin of a (Django) site is generally considered to be part of the backend - it is not the part of the site the user will see - but part of the admin is definitely the View part of the MVC model. Anything a normal web user sees and/or directly interacts with is part of the frontend, everything else is part of the backend.

Now what is the MVC framework used in Django? We have:

  • The Model: this is the part of the application that holds the state of the application. In Django, a big part of this is the database and it's abstraction layer, the Django models. Other parts are user sessions and the request variable.
  • The View: this is the part of the application that presents the state of the application to the user. Django views and templates are responsible for this. Any data you see when you open up the website is the View part of MVC. The overall presentation is also part of this.
  • The Controller: this is the part of the application that represents any action you, the user, takes. Django is not truly a separated MVC framework because the View part and the Controller part are so tightly interwoven: any link, form or button you see on the site is a controller. It tells the site to do an action, such as present a different view (e.g. a link), or change the state of the model (e.g. an edit form).

What about Backbone or Angular? Why do you need two different MVC frameworks in a single application?

Django is a server-side framework. Every action happens on the server. If you click a link or submit a form, you send a request to the server and the server sends back a complete, static response (static in the sense that the page doesn't change once it's in your browser). You can't use Django to use logic client-side, as it is a python framework that runs on your server, not in your client's browser. Instead, it's Javascript's job to add any client-side logic, e.g. to reorder a list of items on the page or to dynamically add a new one. Now each page can be seen as some kind of mini-application.

Backbone and Angular are examples of MVC frameworks for such client-side applications. It supplies the client-side application logic that a server-side framework such as Django lacks, and surprisingly the people who like an MVC framework to develop a server-side application generally also like to use an MVC framework to develop a client-side application.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • Is it possible to use a client-side MVC that interacts with the server, without using any server-side scripting (via PHP or server-side MVC)? After all, AJAX can interact directly with the server, no? – thanks_in_advance Jan 21 '15 at 19:10
  • 1
    @user1883050 Yes it is - AJAX is little more than browsing to an url and handling the response in Javascript, instead of opening it in your browser. You can use AJAX to fetch static snippets of HTML from the server without any server-side scripting involved. It is even possible to create a Javascript application that directly interacts with the database, eliminating the need for _any_ server-side scripting - though that would raise a lot of security issues. – knbk Jan 21 '15 at 19:35
0

Django is a kind-of hybrid version of the Model-View-Controller model. The Django documentation generally describes it as a Model-View-Template model instead. Generally, the template (Django HTML with template tags, etc.) is generally matched to the normal View, providing the user's view in the way of web pages. The view in Django generally takes the place of the Controller, as it works between the Model, taking data from databases and defining new objects, and the View, which in this case is the Template. The Model is the same as normal in Django, providing the definition for different objects. So while the Model-View-Controller is normally the model for most languages, it is more so a Model-View-Template model, with the View being different than it is normally. Read more below:

http://jeffcroft.com/blog/2007/jan/11/django-and-mtv/

mattpic
  • 1,277
  • 9
  • 13
0

I am looking at Django, and patterns for web development.

What I am favoring at the moment(2014-01) is.

Use Django (restful/json) as the MC, Model/controller, or backed data and logic. The controller part in Django refers to business rules and access control.

then use a javascript framework and bit of html as the client side code. View/controller.

In practice, the client/browser loads a javascript program, View/controller that then does restful queries to the backed model/controller

Pieter
  • 1,916
  • 17
  • 17