Javascript MVC Frameworks and Server Side Frameworks - django

This question is regarding the co-extisense of two MVC Frameworks (on the client side and the server side).
Some Background: I am fairly new to web development, but am a very proficient programmer. I learnt javascript and Django only recently. The Front end for my app is quite Javascript extensive and hence I decided to use a framework. I am currently reading up on the available frameworks and I am inclining towards backbone.js + jquery.
My problem is that I don't fully understand how a MVC framework like backbone.js can co-exist with a server side framework like Django.
From what I understood of backbone.js, when there is a change to a model, the corresponding views related to the model are automatically updated. Here, we are referring to the "views" of the Client side framework right? Would such a "view" communicate with the backend framework's "controller"?
How would a possible directory structure be? Is such an architecture bad?
Does the question make sense? or am I lost?
Edit: To add some clarity: I guess my essential question is : How a complex single-page JS web application be structured on the client-side (espertially if u are using a client side framework)? Specifically I'm curious about how to cleanly structure the application in terms of its model objects, UI components, any controllers, and objects handling server persistence.

I can speak in terms of Rails/Backbone, but I can't imagine that Django would be much different.
First, a client-side MVC framework like Backbone isn't just for single-paged apps. You can also use it to add some rich interaction to one or many views of a more traditional app. They simply provide structure and data abstractions on the client.
Next, these client-side frameworks are designed specifically to work with your back-end MVC frameworks. Backbone.js (since you tagged it specifically) models and collections work with REST services. They will talk via GET/POST/PUT/DELETE verbs and will ultimately communicate with your controllers on the back-end when they make asynchronous requests.
In the case of Backbone, it talks JSON instead of HTML. In the case of Rails, this is really easily handled in the controller. If the request is an HTML one, then you return a view as HTML. If it is a JSON request (*.json or Content-type) then the controller returns a JSON representation of the data. I am assuming that it is as easy in Django as it is in Rails to have the same controller respond to multiple content requests (HTML, XML, JSON, etc)

Related

what parts of django MVC becomes irrelevant when using django + REST framework?

Started learning Django lately.
To make long story short - If I choose to combine:
django framework in my server side
REST as the middleware layer
some of the client side frameworks (such as React, Angular, etc)
which of django's MVC components will become irrelevant?
I presume that the templates components. Are there any other fundamental components (model/view ...) that won't be necessary in this case?
Assuming based on your post that you are using Django just to pull data from a database and serve it back to a client in JSON format, and that your templates will be rendered client-side (using, e.g. Angular) then you are correct that you likely won't be needing Django templates. However, you would still need some sort of models (whether you use Django models or something else) and also would need controllers (which Django calls views) in order to:
Do URL routing (that is, bind some URL to some controller/view
function).
Do some kind of server-side processing. Even if your app
is a single-page app and does a lot of client-side processing,
you'll still likely need to implement different kinds of business
requirements and validation on the server side. Some of these
requirements you can likely attach to the models, but others you may
need to implement in controllers.
So while your controllers (aka views) may be a lot "skinnier" due to the way you're structuring your app, they'll still be necessary to some extent. Models will always be necessary if you want some clean and consistent API to your DB.
EDIT: To expand more on this--while there is a Python library called Django REST Framework, it's really just there to assist you in building RESTful APIs. You can certainly build a RESTful API yourself using Django without leveraging it or any additional libraries. As the answer from user D. Shawley states in response to this question -- What exactly is RESTful programming? -- a RESTful API is basically just one where resources are identified by a persistent identifier (in this case, URIs), and where resources are manipulated using a common set of verbs (in this case, HTTP methods like GET, POST, DELETE, etc). So using this idea of URIs as nouns and HTTP methods as verbs, your Django framework might support the following RESTful operations:
GET https://your-app.com/product/123 - this operation fetches a product identified by the ID 123
POST https://your-app.com/product - this operation creates a new product
PUT https://your-app.com/product/123 - this operation updates a product identified by the ID 123
DELETE https://your-app.com/product/123 - this operation deletes a product identified by the ID 123
The data that come back from these operations doesn't necessarily need to be in any particular format (be it JSON, XML, or something else). In an application that closely adheres to the principles of REST, the client (consumer of your RESTful API, in this case your front-end app) would be able to specify (using the HTTP Accept header) which format they want to consume the data in.
I hope that's not too confusing, but really I want to make it clear that REST architecture is just a set of principles, and APIs that web programmers develop may not necessarily adhere to these principles 100%. Whether it's necessary for your application to strictly adhere to RESTful principles depends on your particular requirements. One question to ask yourself then is what are you hoping to accomplish by building a RESTful API using Django? For a lot of developers, the answer is simply "so that I have an easy-to-use interface for my Angular/React/etc. app to retrieve and update server-side resources."

Django or DjangoREST for a server of a pure group chat android app

I am setting up a server for my android chat app. It is a pure group chat i.e. each message will be sent to multiple users and not just a single user
Since I have a background in Python, I would prefer to pick a python based web framework.
So as I was searching, I came across "Django" and "DjangoREST". After reading a lot about them, it seems that DRF (Django Rest Framework) is a RESTful implementation of Django and will let you make applications that are light and scalable.
Since this is for the first time that I am setting up a web service, I can not relate or understand this completely. Also, I feel that what I want to accomplish, could be accomplished on either of the 2 platforms.
Therefore, it will be great if some one could share some very basic and key differences(if any) between the 2 frameworks. So that I can decide which on to pick up.
There are no differences. Django is a web framework and Django Rest Framework makes it easier to create REST services in Django itself.
It doesn't replace Django. It adds to it.

Using AngularJs (WebService) instead of Django View-Template

We have developed a django app and finished all view and templates. There are many controls in template and views. Our new frontend partner says us :
I want to use angularjs, so develop a webservice for me.
So, what is advantages and disadvantages ? Why we might omit the written view and templates?
Any help with this issue will be appreciated.
AngularJS is modern javascript framework for creating complex web
applications within the browser. It focuses on strong separation of
concerns (MVC) and dependency injection to encourage creating
maintainable (and testable) modules that can be integrated to develop
rich client side functionality.
With the advent of more interactive web applications, a ReSTful API
can be leveraged by rich client interfaces to expose and interact with
your application's data model. AngularJS is a great companion because
of its clean separation of controls. AngularJS's modular architecture
involves a little bit of setup. Your app is composed of modules
which define services, directives and controllers that
compartmentalize the functionality for cleaner separation.
Part of the power of AngularJS, is that it provides reactive
programming leveraging their javascript-like expression language.
We can simply define a template that references variables, and our page will automatically refresh when changes are made to those
variables.
Django Rest Framework is best choice to expose your web services/apis as it uses less code.
i might think you have to omit/keep aside the existing views/urls as it uses django forms to store the data etc. Whereas you have to handle the json data (applicable if you not use django rest framework)
reference link

using backbone/ember makes django being a simple REST API?

I have read a couple of articles about using new JS frameworks like Backbone.js or Ember.js
I have come up to this statement:
If I use a JS framework like Backbone.js/Ember.js, I then move the logic from the back-end (Django) to the front-end.
Therefore, will Django actually be used only for its Models?
Does that mean that Django Views and Django Template are not needed anymore and the Django back-end is kinda turn into a "basic" REST API that will be consumed by the front end.
Do you agree? Is it then the purpose of Django in this case?
Is turning the django backend into a REST API one of the most suitable use case when using a framework like Backbone.js/Ember.js for the front-end?
Thanks.
Django is perfectly fine to be used this way, you still get the admin, the models, the orm and all the third-party plugins. However, it isn't blazingly fast, so if you're doing simple document level, non-relational REST mapping, you might wan't to look into node.js and mongoDB for instance.
If you're sticking with django (like we are, we like the structure it gives us), you can use one of the REST plugins:
Django Rest Framework A perfect match since DRF 2.0, under very active development!
Django Tastypie (checkout backbone-tastypie.js for integration)
Django Piston (might be a bit stale or has development picked up lately?)
If you only want to work with frontend development, checkout the Backend-as-a-Service places like cloudmine.me or firebase.com that handle all backend stuff for you, for a price of course.
Django may seem unnecessary once you start thinking about single page solutions and Javascript applications, but if you want your site to be 'fail proof' it wouldn't be impossible to develop both a client side Javascript version of the site as well as a backend django side incase the user, or your site's javascript, fails at some level. Of course this requires creating your site twice, and probably isn't needed in the age of modern browsers, but such would be one of the few instances where you would mix the two for a complete solution.
Yes, that's about it. You can use it to manage authentication to resources and such and maybe use a main view for your application but you won't need to use the server side templating since these frameworks are made to work with json/xml response.
That's why a lot of people are moving to lighter backend/backbone or ember combo instead of a complete solution like django. You can also use your django for caching json response which makes your application appears faster.
We are doing that and use django-piston to make it easier on you.
Normally you make your entire website under Django and only one page will be a "single app page" using backbone.js, usually that page is a very interactive page, with lots of small updates that occur frequently and need to be shown very fast to the user. This page, because of the large number of changes and user interactions is constructed on the client-side so that you are using his PC resources and not the server's, the rest of the pages can use django because it offers you a very stable and secure framework for the server-side

Appropriate back-end for a single-page web application?

Historically I've mainly written web apps in Django, but now I'm increasingly finding that I want to write single-page web apps using Backbone.js or a similar JavaScript framework, with a back-end that solely consists of a database and an API.
My questions is this. If my application structure looks like this:
1. Database
|
v
2. API methods
|
v
3. Single-page front-end written with Backbone
and I'm most comfortable in Django - but also keen to learn new things too, such as NoSQL and Node, if they are appropriate - what would people recommend I use for (2)?
Typically I would use Django with Piston as the API app, but it seems rather heavyweight to have all of Django and only use it as an API provider. Perhaps I shouldn't be worrying, though.
If you use Django, which is an MVC framework, and use Backbone, you might be comfortable setting up your app in pure Node.js or Express.js, with additional modules for connecting to your choice of database.
With Express, if you plan to serve only JSON via RESTful interface, you don't even need to use Views, which is handy. You would only need to set up models and routes (that also serve as controllers).
Any server side framework or lang that is capable of supporting or providing for RESTful APIs should work. I myself as using Slim PHP right now. But seeing that you are from a Django/Python background perhaps this post would be helpful to you.
Recommendations of Python REST (web services) framework?