Skip to content

Django - Creating a Project

Once you have Django installed (see Environment Setup), you can start building web applications. In Django terminology, a Project represents an entire web application or website. A project is composed of one or more Apps. An app is a self-contained module that provides a specific piece of functionality (e.g., a blog, a user polling system, an authentication system). This modular structure promotes code reuse and organization.

Open your terminal or command prompt, navigate to the directory where you want to store your code, and run the following command:

$ django-admin startproject myproject

Replace myproject with the desired name for your project. Avoid using names that conflict with built-in Python or Django components (e.g., django, test).

This command creates a myproject directory with the following initial structure:

myproject/ <-- Outer container directory (can be renamed)
├── manage.py <-- Command-line utility for project interaction
└── myproject/ <-- Actual Python package for your project
├── __init__.py <-- Marks this directory as a Python package
├── asgi.py <-- ASGI entry-point for async web servers
├── settings.py <-- Project configuration settings
├── urls.py <-- Project-level URL declarations (Table of Contents)
└── wsgi.py <-- WSGI entry-point for web servers
  • Outer myproject/ directory: A container for your project. Its name doesn’t matter to Django; you can rename it freely.
  • manage.py: A command-line utility that lets you interact with this Django project. You’ll use it frequently to run the development server, apply database migrations, create apps, run tests, etc. Get a list of available commands with python manage.py help.
  • Inner myproject/ directory: The actual Python package for your project. Its name is used in Python imports (e.g., import myproject.settings).
  • __init__.py: An empty file indicating that this directory should be treated as a Python package.
  • settings.py: Contains all the configuration for your Django project, such as database settings, installed apps, middleware, template engine options, static file locations, security keys, etc.
  • urls.py: The main URL dispatcher for the project. It routes incoming URLs to the appropriate views. Think of it as the table of contents for your Django-powered site.
  • wsgi.py: An entry-point for WSGI-compatible web servers (like Gunicorn, uWSGI, mod_wsgi) to serve your project. WSGI (Web Server Gateway Interface) is the standard for synchronous Python web applications.
  • asgi.py: An entry-point for ASGI-compatible web servers (like Daphne, Uvicorn, Hypercorn) to serve your project. ASGI (Asynchronous Server Gateway Interface) is the standard for asynchronous Python web applications and also supports WSGI applications.

The myproject/settings.py file is crucial. Initially, you might need to configure:

**`DEBUG`**: `DEBUG = True`

Enables debug mode, which provides detailed error pages. Crucially, never deploy a site with DEBUG set to True in production! It exposes sensitive configuration details. Set it to False before deployment. You’ll also need to configure ALLOWED_HOSTS when DEBUG is False.

DATABASES: Configures database connections.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3', # BASE_DIR is defined at the top
}
}

The default uses SQLite, which stores the database in a single file (db.sqlite3 in the project root). This is convenient for development. For production, you’ll typically use more robust databases like PostgreSQL or MySQL. You’ll need to change the ENGINE (e.g., django.db.backends.postgresql, django.db.backends.mysql) and provide NAME, USER, PASSWORD, HOST, and PORT accordingly. You also need to install the appropriate database driver (e.g., psycopg2-binary for PostgreSQL, mysqlclient for MySQL).

Other important settings include:

  • INSTALLED_APPS: Lists all Django applications active in this project.
  • MIDDLEWARE: A list of middleware components processed during the request/response cycle.
  • TEMPLATES: Configuration for the template engine.
  • STATIC_URL, STATIC_ROOT, STATICFILES_DIRS: Settings for managing static files (CSS, JS, images).
  • MEDIA_URL, MEDIA_ROOT: Settings for managing user-uploaded files.
  • SECRET_KEY: A unique secret key for cryptographic signing. Keep this secret in production!
  • TIME_ZONE, LANGUAGE_CODE, USE_I18N, USE_L10N, USE_TZ: Internationalization and time zone settings.

Navigate into the outer myproject directory (the one containing manage.py) in your terminal and run the development server:

$ python manage.py runserver

You should see output similar to this (version number and date will differ):

Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 11, 2023 - 16:30:00
Django version 4.2.5, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-C.

Open your web browser and go to http://127.0.0.1:8000/. You should see the default Django welcome page, confirming your project is set up correctly.