Moving from PHP/Laravel to Python/Django
Question: I want some clarity. I want to learn more about django and use it as
replacement for php/laravel. But the default structure and convention of
django confuses me a bit.

My PHP/Laravel project has 3 parts:  
\- Administration  
\- Core (Web app for regular users)  
\- API Service (REST-API for mobile apps)

However all of controllers, models and views are contained in a single Laravel
application. I separated Auth, Admin, Api controllers into their own
folders/namespaces.

One thing that confuses me is the default Django structure 1 view 1 model
file. How should i go about reworking this application in Django should each
of my controllers be a separate app in my django project or should I have same
approach as in Laravel. 3 Django apps in one project one for admin one for
core and one for api ? Where should I keep my models than since in Laravel all
models are used by all 3 parts ?

My current structure:

    
    
    ./  
      ./controllers/
        ./auth/  
          LoginController.php  
          RegistrationController.php  
          ...
        ./admin/    
          ReportsController.php  
          UserController.php (Admins overview of all users)    
          ...
        ./api/  
          HealthController.php (API CRUD for Health resource)  
          ExerciseController.php  
        HomeController.php  
        UserController.php (Regular users profile page CRUD)    
        ...
      ./models/  
        User.php  
        Health.php  
        Exercise.php  
        ...  
    


Answer: One thing to remember about Django is that an app in Laravel doens't necessary
translate to an app in Django. In Django, there are projects, and each project
can have any number of apps. For example, I have a "Backup Admin" project
where I manage a lot of the day-to-day issues of managing a tape backup
environment. I have an app for media (that has 3 models, one for regular
media, one for cleaning media, and one for media that we want to exclude from
tape ejections). I have an app that represents the backup images, and another
for backup jobs (to check status codes). Each sub-piece of my project goes
into another app.

If I wanted to do another Django project that had nothing to do with backups,
I'd make that a completely separate project, which would have a separate
directory structure from my backup project. It'd have it's own urls.py,
settings.py, etc.

Regarding the models piece, I put all of one app's models in the same file.
For example, in my media app, I have models.py, which contains all three
models that I mentioned above. This is completely optional, but I do it just
so while importing these models into other parts of the project, I don't have
to remember what the file names are, instead I can just do this:

    
    
    from media.models import CleaningMedia,Media,EjectExclusions
    

Otherwise I'd have to have 3 different import statements if they were in
different files. It's completely possible, but based on your preferences.

Regarding the controller, Django lets you do it either way. You have a
project-wide urls.py file that you can use to control all of the traffic, or
you can have separate urls.py files in each app to control that app's traffic.
I prefer a single file, but that's just me. Personally if you have a lot of
controller entries, you should probably split them up into app-specific
urls.py files, just to keep it clean, but again, either method would work. I
think of maintainability (especially with respect to teammates having to
support it) when I make these types of decisions.

The admin interface is built-in, so there's not really an app for that, but
you can decide which models and which apps have entries on the admin interface
quite easily. Each app has an admin.py file that controls this.

A side note, for a RESTful API, you also might want to consider Django Rest
Framework. It's a great piece of software, and the documentation (and
tutorials) are very helpful.

Edit: The 1 view/1 model thing again is just preference. You can have as many
files as you want. The only trade off is when you import them into other
files, you have to specify the file you're importing it from. That's really
all there is to it. I know people who have a views/ directory, and inside
there, have separate files for each view, keeping each class/function
separate. Totally a matter of preference.

