What version of Python should be used?

I recommend that you use the most recent version available — at time of writing this is Python 3.7.2.

Python 3.5 or later can be used if needed (Python 3.5 support will be dropped in future releases).

Packages:

Let's Start

* You can checkout this repository as a reference for source code: https://github.com/nitinraturi/django-structure

First, change your directory to where you want to setup your project.

In my case, directory structure is something like this:

src/
venv/

If you are using virtual environment, make sure to activate it.

source venv/bin/activate

Change your directory to src for setting up the project.

cd src

Installation

pip install django==2.2.2

Project Layout

I don’t like the default django layout that results after we are prompted to give our project a name like myproject, which also happens to be the name of our repo, and we end up typing things like ~/code/myproject/myproject/settings.py, which is just simply awful. It makes much more sense to me to put all your configuration files in a directory called conf. Your project’s name is already the name of the root directory. So, let’s start our projects by

Start a django project by running:

django-admin startproject conf .

Now, create few custom folder for our layout inside src/. 

mkdir {apps,static,static_cdn,templates}

Now, inside static_cdn create two more directories for static files and uploaded files:

cd static_cdn/
mkdir {static_root,media_root}

Now, if you ls  in your terminal inside src/, your directory structure should be something like this:

apps/
conf/
--- __init__.py
--- urls.py
--- settings.py
--- wsgi.py
static/
static_cdn/
--- static_root/
--- media_root/
templates/
manage.py

Structure explaination

apps/: Here you can install your django apps, instead of installing them to the root directory. After install any app you can put them in the settings.py like this:

# Command to create in an app
python manage.py startapp mysite

# In settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #custom_apps
    'apps.mysite',
]

static/: This is where you will put all your static files like css, js.

static_cdn/: This is where you will put production related static files or files uploaded by users.

  • static_root/: Here, django will put all static files when you will run 'python manage.py collectstatic' command
  • media_root/: Here, all the uploaded files will be stored.

Configuring settings.py and urls.py

# conf/settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR,"static_cdn","static_root")

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static_cdn',"media_root")
# conf/urls.py

from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    
    # [Optional] This is how you will link your custom app [Optional]
    # Make sure you have urls.py in your custom app.
    path('mysite/',include('apps.mysite.urls',namespace="mysite")),

]


if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Finished.

Testing your installation

python manage.py runserver

#Output

Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 23, 2019 - 12:09:30
Django version 2.2, using settings 'conf.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
 
*Instead of re-inventing the wheel, you can clone this repository for pre-configured django project: https://github.com/nitinraturi/django-structure