understanding django project structure

understanding django project structure

In the previous article we have seen how to setup the django development environment. In this article we will learn about how to create django proect and django project structure. We care about project structure because it helps us to organise the complex project code. A well structured project will helps the developers to navigate through the code quickly.

Create django project

To create django project open the terminal(ubuntu) or command(cmd) prompt and change the directory to a new directory where you want to create the project. Now, run the below command.

django-admin startproject my_project 

Above command will create the django project with project name "my_project" and we can see the below project structure.

├── manage.py
└── my_project
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Let's talk about django project structure

  1. manage.py:
    • It is a python module it provides the command-line utilities for django project just like django-admin.
    • To know the commands that provided by the "manage.py" then just run the command. It will show us all available commands.
      python manage.py --help
    • Most commonly used command is "runserver". It is used to run the development server.
  2. my_project: It is the python package of our project. It contains the configuration files of project settings ,  urls and wsgi.
    1. __init__.py:  It's a python file which tells the python that the directory is a python package.
    2. settings.py:  It's settings configuration file for the project. It contains the settings like database, urls, usgi and etc.
    3.  urls.py:  It base file of urls for django application. It is pointed in the settings.py file using the setting 'ROOT_URLCONF'.
    4. wsgi.py:  It is file which contains the configuration of wsgi application. It is used to run the django application. It is pointed in settings.py file using the setting 'WSGI_APPLICATION'.

Let's talk about settings in settings.py

  • DEBUG: It is set to boolean "True" in development mode and to boolean "False" in production mode. It will enable server to show us errors that helps developers to debug and fix the error if DEBUG set to "True".
  • ALLOWED_HOSTS: It is a security measure in django. It prevents the HTTP Host header attacks. It takes a list of string that represents the host/domain names that django application can serve.
  • INSTALLED_APPS: In django, we divide complex projects in sub projects and develop them to make it simple. we call these sub projects as apps. we put all these apps in this setting. django considers database migrations only for the apps which are in INSTALLED_APPS setting. 
  • ROOT_URLCONF: It is the base point for urls. When a request comes in django routes the request based on the url patterns.
  • DATABASES: In django, database settings are used to connect to the database for information storing and retrying.
  • TEMPLATES: when a request comes in we generally create an http response using a html template. In django template settings we provide template directory paths and template engines. Template paths are used to find the given template name and template engines are used to render the templates.
  • STATIC_URL: It is a prefix added to the static files path when we use "static" template tag in a template.
  • STATICFILES_DIRS: When we define a static resource in templates then it has to be served. In order to serve it django has to look in some directories. django uses this setting to look for static files.
  • STATIC_ROOT: When deploying the django app we serve staticfiles with servers like nginx, apache.  These servers takes a directory path where all staticfiles resides. To do it in django we have a command "python manage.py collectstatic". When we run this command it will collects all static files to the STATIC_ROOT directory.
  • MEDIA_URL: It's prefix added to the medial files path when a media resource is specified in templates.
  • MEDIA_ROOT: It's a directory path where all media files will be stored. When a user uploads a file then it has to store somewhere. In general files deals with large amount of memort.So, we do not store files in databases instead we store it in filesystem.

Above mentioned settings are basic settings that we use in django while developing the applications. In coming articles we will be discussing other django settings also. 

Read Next: understanding Model View Controller(MVC) in Django

Read Prev: usage of property decorator in python

Blog Archive