Django project, apps structure and folders

Joey Masip Romeu
2 min readDec 6, 2018

--

In this blog post I’ll talk about Django folders structure inside a project.

After developing a few projects with Django 1.11 and Django 2.0, I’ve stumbled with somewhat an issue that’s been bothering me. When you create projects and apps in Django, as the tutorial shows you, apps will be created inside the main project folder, at the same level of the project’s settings folder (usually it’s the folder that has the same name as your project).

So basically, what you end up with is a container folder and inside has the project config folder, and many other folders with the app names.

So as a result, you get different ordering in the folders for every new project, as folders will usually order by name inside your IDE, i.e PyCharm.

So, for instance, if your app is called ‘apples’ and your project is called ‘mysite’, then the order will be:

- apples
- mysite

Let’s also assume you’re using docker, so you probably have a docker folder, now we have:

- apples
- docker
- mysite

But if you create an app called ‘oranges’, now your project folder is:

- apples
- docker
- mysite
- oranges

This is unconfortable because you never know which is the project’s config folder and which ones are the different app folders, so you keep opening the wrong folders all the time!! This is awful, as programers, we tend strive for efficiency!! :)

So my strategy for now is to bundle all apps inside an apps folder, so my project will always look like:

- apps
- docker
- mysite

And there you have it, nicely ordered apps inside your project!!

To include them in your settings, you just have to remember to include the namespace

'apps.your_app_name'

, so if your app is called apples

INSTALLED_APPS += ['apps.apples']

Also, when importing classes in different files, just use the namespace. So imagine, I need to import the Apple class defined in my models inside my apples app, would do something like so:

from apps.apples.models import Apple

If you’ve read until here, it means you REALLY CARE about your project folder structure, so here’s a handy script I’ve created to start apps. It’s just a wrapper from the django startapp command, but it takes care of the apps folder, etc.

#!/usr/bin/env bash

if [ "$1" == "-h" ]; then
echo "This script will create an app inside the apps folder"
echo "To use type the following line:"
echo "bash start-app.sh app_name"
echo "Replace app_name with the actual name for your app"
elif [ "$1" != "" ]; then
if [ ! -d "apps" ]; then
mkdir apps
touch apps/__init__.py
fi
mkdir apps/$1
if [ -f /.dockerenv ]; then
python manage.py startapp $1 apps/$1
else
docker-compose run django python manage.py startapp $1 apps/$1
fi
echo "Success! The app $1 has been aded, don't forget to add INSTALLED_APPS += ['apps.$1'] in your project's settings.py"
else
echo "Error! One parameter is expected: app_name"
fi

I call the file “start-app.sh”, so to use it just type

bash start-app.sh app_name

Happy coding!!

Originally published at Joey’s blog.

--

--