Home > OS >  Comparing Django project structure to ruby on rails
Comparing Django project structure to ruby on rails

Time:12-02

After some years developing web apps using ruby on rails, I decided to give Django a try, however it seems that I'm missing something, which is how to structure large project, or any project in general.

For example, in rails we have a models folder which contains model classes, each in a separate ruby file, a controllers folder which contains controller classes, again each in a separate ruby file.

However, in Django it split the project into independent apps, which can be installed independently in other Django project, each app has a models.py file which contains all the models classes, a views.py file which contain all the views functions.

But then how to group functions in views like rails? That is one controller per each model.

In general how to structure my project when it contains one large app that can't be separated into multiple independent apps? I want for example to have a view index function for each model, but how to do this if all functions are in one file?

If my project is about selling cars for example. I should have index function that maps to /cars, another index function to map to /users, etc...

I searched the web but couldn't find a suitable answer.

It is unclear to me how to structure Django app, so any help will be appreciated.

CodePudding user response:

In short, Django is a Model-View-Template framework and Rails is a Model-View-Controller framework.

In Django we store controllers(sort of) in views.py for each specified app, while in MVC framework such as Rails store it in controllers. In Django, you also have to create your own HTML template separately which some people may find it tedious but its easier to work with other frameworks such as Vue or React due to that separation.

This is general comparison I found on the net.

However, to answer your questions on folder structure. Basically Django is very flexible on folder arrangements, it really depends how you want to design the project structure. Normally what I'd do is keep every app in the main folder (project folder). This way you wont mess with the venv setup

CodePudding user response:

As mentioned in @shanksfk's answer, Django is very flexible in folder arrangements. You don't have to follow the default app structure. When I create a purely backend Django project (with DRF), I usually have 3 base apps:

  1. api - where modules, serializers, and urls are stored
  2. core - the default app (the one that has the name of your Django project)
  3. db - where models are stored

Then as I expand, I can add a folder dedicated for the helpers, utils, and possibly abstraction layers for external services. I recommend reading more about Domain-driven Design to get an idea on how to structure your project. You can also check other Django projects for inspiration:

  • Related