-->

django project layout and settings


Let's talk about "Django" project structure in depth. You can see the complete project layout in above image.

base/project/: It is a container for our project. we can give any name to this directory and we can modify it at any time.
project/manage.py: It is created for all django projects. It is a command-line utility, it is used to interact with the "django" project in many ways.
We use below command to run development server
python manage.py runserver
"runserver" is a command which is used to run the server.
To show all available commands run the below command
python manage.py
project/project: This directory contains the project files.
project/project/__init__.py:  It is a empty file that tells python to consider the directory as a package.
project/project/settings.py: It contains all configuration settings like database settings, installed apps, root url conf etc.
project/project/urls.py: It is the root url file. It contains url patterns to map the request to the respective view to process it.
project/project/uwsgi.py: An entry-point for WSGI-compatible web servers to serve our project.
project/static: It contains the static assets like css, javascript, images, etc.
project/templates: It contains the html files that will be used to create http response to a request.
project/media: It is a path to store the media contents like files that are uploaded by users.
To get project structure that is shown in above image. we need to update settings.py file with below code.

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR + '/media/')
MEDIA_URL = '/media/'
STATIC_URL = '/static/' 
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ] 

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["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',
            ],
        },
    },
]
To know more about customizing settings visit: https://docs.djangoproject.com/en/1.10/ref/settings/
project/app1: It is a python package, It contains application related files.
project/app1/templatetags: It's a directory that contains template tags of our application. Template tags helps us at the time of rendering the template. It takes the context from the template and processes it and again return it to the template.
project/app1/templatetags/__init__.py: It's empty python file, it tells python to recognize the directory as a python package.
project/app1/templatetags/tags.py: It's a python file, it contains the custom template tags that we will use it in templates.
project/app1/apps.py: It contains the application's configuration.
project/app1/models.py: It contains the models(db tables) of application. we represent our database tables with the model in django. ORM(Object Relation Mapper) converts the models to db tables. We do not use raw SQL(Structured Query Language) to query the database, instead we use django queries. ORM convert these queries into SQL equivalent code and performs the db query and again converts the relational data to python objects to make things easier.
project/app1/migrations: It contains the database migrations of application, each migration contains the database schema related code.
project/app1/migrations/__init__.py: It's empty python file, it tells python to recognize the directory as a python package.
project/app1/migrations/001_initial.py: It's a migration file of application. It contains the database schema related code, that is used to create database schema.
project/app1/__init__.py: It's a python file, it tells python to recognize the directory as a python package.
project/app1/admin.py: Django provides the inbuilt admin panel. This file contains the admin related code.
project/app1/urls.py: It contains url's that are related to the application.
project/app1/views.py: It contains the views(functions/classes) that are mapped from the url's to process the request.
project/app1/tests.py: It contains the unit tests of the application.

Buy a product to Support me