[Solved]-Create a separate app for my REST API or place it inside my working app?

13👍

Although a case can be made for either of the approaches, I think keeping the APIs inside their associated apps would be a better one. Since the code in APIs is going to depend on the models, or other utility methods anyway, keeping APIs in the same app would lead to more cohesive code. Besides the very ideology behind Django apps is that they can be isolated and reused.

There used to be a similar case with storing the templates. In the initial days of Django, people used to prefer to store all the templates altogether in the same global folder (with subdirectories by the names of the app), however, in recent times even Django has started discouraging the said approach in the favour of storing templates in the respective app itself.

4👍

@hspandher’s answer is very solid and will allow for most of your needs to be implemented.

There is though another approach which may be a bit more complicated to achieve but gives you all the space you may need for experimentation and reusability potential:

Separate everything:

  1. Backend:

    Isolate your API from its visualization (see frontend below) and make it completely autonomous and self-contained.
    That can be achieved by separating your apps inside your Django project and expose the corresponding APIs which must be the only way for an external factor (ex. client, another app etc.) to “talk” with any one of your apps.

  2. Frontend:

    Assuming that you have your APIs exposed, you effectively separated the visualization from the logic and therefore you have many options on how to visualize your maps.
    For example, you can now build a React app which can make requests to your API and visualize the responses by using any of those tools: leaflet.js, D3.js, or anything that you like really.

Summary:

The benefits of this separation are:

  • Separation of logic and implementation.
  • Better maintainability.
  • Many tool and technology options to use.
  • Reusability.

As a side note, you can read about 12 factor method and think about using it in your implementation.

Leave a comment