0

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?

GyRo
  • 2,396
  • 4
  • 27
  • 38

1 Answers1

0

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:

  1. Do URL routing (that is, bind some URL to some controller/view function).
  2. 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."

nb1987
  • 1,292
  • 1
  • 12
  • 12
  • your answer clarify me that I need to understand better what django together with REST framework means at all – GyRo Apr 07 '18 at 20:51
  • 1
    I have added a fairly lengthy edit to my post. I hope that clarifies things, but if not I am happy to try to explain further. – nb1987 Apr 07 '18 at 21:09