← Back to Python | Main README
This directory contains a fully production-ready Django project implementation. Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This project demonstrates best practices for deploying Django applications in production environments.
- Environment-based Configuration: Using python-decouple for managing environment variables
- REST API: Full-featured REST API using Django REST Framework
- Database Support: PostgreSQL for production, SQLite for development
- Static Files Management: Whitenoise for efficient static file serving
- Security: Production-ready security settings (HTTPS, HSTS, XSS protection)
- CORS Support: Cross-Origin Resource Sharing enabled
- Admin Interface: Customized Django admin panel
- Logging: Comprehensive logging configuration
- Docker Support: Full containerization with Docker Compose
- Production Server: Gunicorn WSGI server with Nginx reverse proxy
django/
├── config/ # Project configuration
│ ├── __init__.py
│ ├── settings.py # Main settings (production-ready)
│ ├── urls.py # URL routing
│ ├── asgi.py # ASGI config
│ └── wsgi.py # WSGI config
├── core/ # Main application
│ ├── models.py # Database models (Task, Category)
│ ├── views.py # Views and ViewSets
│ ├── serializers.py # REST API serializers
│ ├── admin.py # Admin customization
│ ├── urls.py # App-specific URLs
│ └── migrations/ # Database migrations
├── templates/ # HTML templates
│ └── core/
│ ├── base.html
│ ├── home.html
│ └── task_list.html
├── static/ # Static files (CSS, JS, images)
├── staticfiles/ # Collected static files
├── media/ # User-uploaded files
├── logs/ # Application logs
├── manage.py # Django management script
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── Dockerfile # Docker container definition
├── docker-compose.yml # Multi-container Docker app
├── nginx.conf # Nginx configuration
└── README.md # This file
- Python 3.8 or higher
- pip package manager
- PostgreSQL (for production)
- Docker & Docker Compose (optional, for containerized deployment)
-
Clone the repository and navigate to the Django project
cd python/django -
Create and activate virtual environment
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
cp .env.example .env # Edit .env file with your configuration -
Run database migrations
python manage.py migrate
-
Create a superuser
python manage.py createsuperuser
-
Collect static files
python manage.py collectstatic
-
Run development server
python manage.py runserver
Visit
http://127.0.0.1:8000in your browser.
-
Navigate to the Django project
cd python/django -
Build and start containers
docker-compose up -d --build
-
Create superuser (in container)
docker-compose exec web python manage.py createsuperuser -
Access the application
- Web Application:
http://localhost - Django Admin:
http://localhost/admin - API:
http://localhost/api
- Web Application:
-
Stop containers
docker-compose down
Create a .env file based on .env.example:
# Django Configuration
SECRET_KEY=your-secret-key-here-change-in-production
DEBUG=False
ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
# Database Configuration (PostgreSQL)
USE_POSTGRES=True
DB_NAME=django_db
DB_USER=postgres
DB_PASSWORD=your-secure-password
DB_HOST=localhost
DB_PORT=5432
# CORS Configuration
CORS_ALLOWED_ORIGINS=https://yourdomain.com
# Security Settings
SECURE_SSL_REDIRECT=True
# Logging
LOG_LEVEL=INFODevelopment (SQLite)
USE_POSTGRES=FalseProduction (PostgreSQL)
USE_POSTGRES=True
DB_NAME=django_db
DB_USER=postgres
DB_PASSWORD=your-secure-password
DB_HOST=localhost
DB_PORT=5432python manage.py runservergunicorn --bind 0.0.0.0:8000 --workers 3 config.wsgi:application- Configure nginx with the provided
nginx.conf - Start Gunicorn on port 8000
- Nginx will proxy requests to Gunicorn
GET /api/- API information and available endpoints
GET /api/tasks/- List all tasksPOST /api/tasks/- Create a new taskGET /api/tasks/{id}/- Retrieve a specific taskPUT /api/tasks/{id}/- Update a taskPATCH /api/tasks/{id}/- Partially update a taskDELETE /api/tasks/{id}/- Delete a task
GET /api/categories/- List all categoriesPOST /api/categories/- Create a new categoryGET /api/categories/{id}/- Retrieve a specific categoryPUT /api/categories/{id}/- Update a categoryPATCH /api/categories/{id}/- Partially update a categoryDELETE /api/categories/{id}/- Delete a category
GET /api-auth/login/- API login pageGET /api-auth/logout/- API logout
python manage.py testpip install coverage
coverage run --source='.' manage.py test
coverage reportpython manage.py test corepython manage.py makemigrationspython manage.py migratepython manage.py flushpython manage.py dbshellAccess the Django admin panel at /admin/ after creating a superuser:
python manage.py createsuperuserFeatures:
- User management
- Task management with filters and search
- Category management
- Customized list displays and fieldsets
-
Change SECRET_KEY: Generate a new secret key
python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())' -
Set DEBUG=False: Never run with DEBUG=True in production
-
Configure ALLOWED_HOSTS: Add your domain names
-
Use HTTPS: Set SECURE_SSL_REDIRECT=True
-
Database: Use PostgreSQL, not SQLite
-
Static Files: Run
collectstaticand serve with Nginx/Whitenoise -
Environment Variables: Use
.envfile, never commit secrets
- Set up PostgreSQL database
- Configure environment variables
- Collect static files:
python manage.py collectstatic - Run migrations:
python manage.py migrate - Create superuser:
python manage.py createsuperuser - Start Gunicorn with systemd or supervisor
- Configure Nginx as reverse proxy
- Set up SSL certificate (Let's Encrypt)
Create /etc/systemd/system/django.service:
[Unit]
Description=Django Application
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/django
Environment="PATH=/path/to/venv/bin"
ExecStart=/path/to/venv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock config.wsgi:application
[Install]
WantedBy=multi-user.target- Django 6.0.1: Web framework
- djangorestframework: REST API toolkit
- django-cors-headers: CORS support
- python-decouple: Environment variable management
- psycopg2-binary: PostgreSQL adapter
- gunicorn: WSGI HTTP server
- whitenoise: Static file serving
- title: CharField(max_length=200)
- description: TextField(blank=True)
- status: CharField (choices: todo, in_progress, done)
- created_by: ForeignKey(User)
- created_at: DateTimeField(auto_now_add=True)
- updated_at: DateTimeField(auto_now=True)
- due_date: DateField(null=True, blank=True)- name: CharField(max_length=100, unique=True)
- description: TextField(blank=True)
- created_at: DateTimeField(auto_now_add=True)- Django project structure and configuration
- Environment-based settings management
- Database models and migrations
- Django ORM and querysets
- Class-based views and ViewSets
- Django REST Framework
- Template rendering with Django template language
- Django admin customization
- Static files and media handling
- Security best practices
- Production deployment with Docker
- WSGI servers (Gunicorn)
- Reverse proxy with Nginx
python manage.py collectstatic --clear
python manage.py collectstatic- Check PostgreSQL is running
- Verify database credentials in
.env - Ensure database exists:
createdb django_db
python manage.py migrate --run-syncdb# Find process using port 8000
lsof -i :8000
# Kill the process
kill -9 <PID>- Django Documentation
- Django REST Framework
- Django Deployment Checklist
- Gunicorn Documentation
- Nginx Documentation
- Docker Documentation
This project is part of the web-study repository and is intended for educational purposes.
Contributions are welcome! Please follow the existing code style and patterns.