Understanding Project Structure in Django


When you create a new Django project, it generates a structured set of files and directories. This guide explains the purpose of each file and folder in a typical Django project.

Step 1: Creating a Django Project

To create a Django project, use the following command:

            django-admin startproject myproject
        

This creates a directory named myproject with the following structure:

    myproject/
        manage.py
        myproject/
            __init__.py
            asgi.py
            settings.py
            urls.py
            wsgi.py
        

Step 2: Top-Level Directory

The myproject/ directory contains all the files and subdirectories for your Django project. It includes:

  • manage.py: A command-line utility to manage your project. You can use it to run the development server, create apps, and perform migrations.
  • myproject/: A subdirectory containing the core files of your project.

Step 3: The manage.py File

The manage.py file allows you to execute various commands. Examples:

            python manage.py runserver  # Starts the development server
            python manage.py startapp myapp  # Creates a new app
            python manage.py migrate  # Applies database migrations
        

Step 4: Project Subdirectory

The inner myproject/ directory contains configuration files for your project. Let's break it down:

__init__.py

An empty file that marks this directory as a Python package.

asgi.py

Entry point for ASGI-compatible web servers to serve your project. This is used for asynchronous communications.

            import os
            from django.core.asgi import get_asgi_application

            os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
            application = get_asgi_application()
        

settings.py

Contains the configuration for your Django project. Examples of settings:

    # Database settings
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
         }
     }

     # Installed apps
     INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
        

urls.py

Defines URL patterns for your project. Example:

            from django.contrib import admin
            from django.urls import path

            urlpatterns = [
                path('admin/', admin.site.urls),
            ]
        

wsgi.py

Entry point for WSGI-compatible web servers to serve your project. This is used for deployment.

            import os
            from django.core.wsgi import get_wsgi_application

            os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
            application = get_wsgi_application()
        

Step 5: Adding an App

Django projects are divided into apps. To create an app, run:

            python manage.py startapp myapp
        

This generates a directory named myapp with the following structure:

     myapp/
        migrations/
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        views.py
        

Step 6: Connecting an App to the Project

To use the new app, add it to the INSTALLED_APPS list in settings.py:

            INSTALLED_APPS = [
                'myapp',
            ]
        

Step 7: Next Steps

With this understanding of the Django project structure, you can now start building apps, defining models, creating views, and configuring URLs to build a fully functional web application!





Advertisement