Sadra
Sadra

Reputation: 384

The best approach to keep API views and Template views in Django

I'm looking for the best approach/practice to keep API views and Template views in Django. Making a separate app called API/ or keeping all views together in the views.py file? There is also another approach that creates a directory within the main app called views/ which includes views/api.py and views/visual.py and simply removes the traditional views.py. Which is better??

Upvotes: 1

Views: 1181

Answers (2)

Elias Prado
Elias Prado

Reputation: 1817

Follow the way I have seen many other projects structured as same as the one we use in our company.

your_app/
├── __init__.py
├── api/
│ ├── __init__.py
│ ├── serializers.py
│ └── viewsets.py
├── apps.py
├── migrations/
├── templates/
├── admin.py
├── models.py
├── tests.py
├── views.py

There is no point you add viewsets and serializers outside of the same app. It will only confuse your team. Nevertheless, if you are creating Mixins and helper functions that may be used across other apps that is the best way living it in a core app for example.

The best way for your understand the best practices is looking at the django repository on github and seeing how they structure their folders. Also, have a read on the book Two scoops of Django there the author explains better how to better structure a django project.

The last but not least, my tough is that if those functions belongs strictly to that app leave them where they belong. Modularity will make it easy to fix future problems and even make it easy if one day that app may become a package.

Upvotes: 2

Rowan Knight
Rowan Knight

Reputation: 451

I think it depends on the project.

If the API is going to be a big part then an app makes sense.

Personally I like the views directory, as you can separate views in to subsections if the project requires that.

Upvotes: 0

Related Questions