################################################### Django overview ################################################### The following picture describes the mechanism of Django : .. image:: django_steps.png .. _django_overview: Overview of Django configuration ====================================================== On the Django installation the following directories are created : .. code-block:: bash myprojectdir ├── manage.py #django utility to manage Django apps ├── myprojectenv #python virtual environment used for Django framework and associated modules ├── myproject #Django installation level directory containing common ressources for all projects ├── static #Django installation level directory containing static *.css, *.js, *.png, ... files for all projects The ``myproject`` directory created automatically at install time contains the following : .. code-block:: bash myprojectdir ├── manage.py ├── myprojectenv ├── myproject #parent Django project containing common ressources for all projects ├── __init__.py #special Python file that indicates that the parent directory should be treated as a Python package ├── __pycache__ #created on first Python code execution and used in subsequent executions so that the program starts faster ├── asgi.py #asychronous processing equivalent to WSGI used to manage the server side invoking a callable object ├── settings.py #contains all the configuration of the Django installation ├── urls.py #contains all mappings between URLs, that the Django installation serves, and Python views. All URLs of all Django apps need to be declared here ├── wsgi.py #Web Server Gateway Interface to process non static requests such as Python code that need to be executed on server side When a new Django app is launched using the ``manage.py`` command, the ``django_app`` directory is created automatically at the same level as the directory ``myproject`` and will have the following hierarhy : .. code-block:: bash myprojectdir ├── manage.py ├── myprojectenv ├── myproject ├── static ├── django_app #directory for each individual Django app ├── __init__.py #special Python file to indicate the directory should be treated as Python package ├── __pycache__ #python cache ├── admin.py #django app admin utility used to customize and configure the Django admin interface for managing models in Django project ├── apps.py #used to configure the application and define application-specific settings and metadata ├── migrations #when changes are done in the database, Django can automatically generate migration files that represent those changes ├── models.py #central component of each Django app, where data models for the application are defined ├── templates #used to store HTML template files that define the structure and layout of the user interface for the web application ├── tests.py #used to write unit tests for the Django application ├── urls.py #used to define URL patterns for the web application ├── views.py #used to define the view functions or classes that handle HTTP requests and generate HTTP responses for the web application .. _activate_virtual_env: Activate virtual env ====================================================== To use Django command line, activate the virtual environement .. code-block:: bash usr1@server:~/myprojectdir$ source myprojectenv/bin/activate The prompt is changed as follows .. code-block:: bash (myprojectenv) usr1@server:~/myprojectdir$ .. _create_new_django_app: Create new Django app ====================================================== We can now execute Django instructions. To create a new Django app, use the ``manage.py`` command. .. code-block:: bash (myprojectenv) usr1@server:~/myprojectdir$ python3 manage.py startapp demo1 This results in the creation of a directory named ``demo1`` with the following hierarchy. .. code-block:: bash myprojectdir ├── manage.py ├── myprojectenv ├── myproject ├── static ├── demo1 #directory for each individual Django app ├── __init__.py #special Python file to indicate the directory should be treated as Python package ├── __pycache__ #python cache ├── admin.py #django app admin utility used to customize and configure the Django admin interface for managing models in Django project ├── apps.py #used to configure the application and define application-specific settings and metadata ├── migrations #when changes are done in the database, Django can automatically generate migration files that represent those changes ├── models.py #central component of each Django app, where data models for the application are defined ├── templates #used to store HTML template files that define the structure and layout of the user interface for the web application ├── tests.py #used to write unit tests for the Django application ├── urls.py #used to define URL patterns for the web application ├── views.py #used to define the view functions or classes that handle HTTP requests and generate HTTP responses for the web application