Django Local Development Guide

Run Python web applications on localhost:8000 using Django, configure SQLite or PostgreSQL databases.


Django is a full-stack web framework for Python, featuring built-in ORM, an admin interface, and an authentication system. For local development, use the runserver command, which defaults to http://localhost:8000.

Default localhost Access

PurposeAddress
Development Serverhttp://localhost:8000
Admin Interfacehttp://localhost:8000/admin
Specify Portpython manage.py runserver 8080

Quick Start

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install django
django-admin startproject mysite
cd mysite
python manage.py migrate
python manage.py runserver

Database

Default SQLite (zero configuration):

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

PostgreSQL (recommended for production and complex projects):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': 'localhost',
        'PORT': '5432',
        'NAME': 'mydb',
        'USER': 'postgres',
        'PASSWORD': 'secret',
    }
}

You need to install: pip install psycopg2-binary

Create Superuser

python manage.py createsuperuser

Access http://localhost:8000/admin to log in to the admin interface.

Comparison with FastAPI / Flask

FrameworkDefault PortFeatures
Django8000Full-stack, Admin, ORM integrated
Flask5000Micro-framework, flexible
FastAPI8000 (Uvicorn)Asynchronous API first

Frequently Asked Questions

Port 8000 Occupied
Switch to runserver 8001; or terminate the occupying process (see the port-conflicts article).

Static Files Not Loading
Django handles this automatically in development; for production, use collectstatic + WhiteNoise/Nginx.

ALLOWED_HOSTS
Usually not an issue with local DEBUG=True; set domain names when deploying.

Summary

Run Django locally with python manage.py runserver and access http://localhost:8000; start with SQLite for the database, then switch to PostgreSQL (5432) as needed.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run