🎉 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
7.4 KiB
Django Demo Project
A production-ready Django application template optimized for VPS hosting deployment. This project serves as a foundation for testing VPS deployment workflows and can be used as a template for future Django projects.
✨ Features
- Minimal Dependencies: Clean, lightweight setup with only essential packages
- Environment Configuration: Production settings managed via environment variables
- Database Flexible: Works with SQLite for development and PostgreSQL for production
- Static Files Handling: Configured with WhiteNoise for efficient static file serving
- Contact Form: Functional contact form with email validation
- Simple Blog System: Basic blog functionality with admin interface
- Bootstrap UI: Responsive design using Bootstrap 5
- Production Security: Security headers, HTTPS support, and production optimizations
- VPS Deployment: Complete deployment scripts and configuration files
🛠 Tech Stack
- Backend: Django 4.2.7
- Database: SQLite (development) / PostgreSQL (production)
- Web Server: Gunicorn + Nginx
- Frontend: Bootstrap 5, Vanilla JavaScript
- Static Files: WhiteNoise
- Configuration: python-decouple
🚀 Quick Start (Development)
-
Clone and setup:
git clone <repository-url> cd hostinger-django-demo python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt -
Configure environment:
cp .env.example .env # Edit .env with your settings -
Initialize database:
python manage.py migrate python manage.py createsuperuser python manage.py collectstatic -
Run development server:
python manage.py runserver -
Access the application:
- Homepage: http://127.0.0.1:8000
- Admin: http://127.0.0.1:8000/admin
🌐 VPS Deployment
Prerequisites
- Ubuntu 20.04+ VPS
- Root or sudo access
- Domain name (optional, can use IP address)
Automated Deployment
-
Upload project files to your VPS:
scp -r . user@your-vps-ip:/home/ubuntu/django-demo -
Run deployment script:
cd /home/ubuntu/django-demo sudo bash deploy/deploy.sh -
Configure your settings:
- Edit
.envfile with production settings - Update domain in
deploy/nginx.conf - Restart services:
sudo systemctl restart django-demo nginx
- Edit
Manual Deployment Steps
If you prefer manual setup, follow these steps:
-
Install system packages:
sudo apt update sudo apt install python3 python3-venv python3-pip nginx postgresql postgresql-contrib -
Setup project:
cd /home/ubuntu/django-demo python3 -m venv venv source venv/bin/activate pip install -r requirements.txt -
Configure environment:
cp .env.example .env # Edit .env with production settings -
Setup database and static files:
python manage.py migrate python manage.py collectstatic --noinput python manage.py createsuperuser -
Configure services:
sudo cp deploy/systemd.service /etc/systemd/system/django-demo.service sudo cp deploy/nginx.conf /etc/nginx/sites-available/django-demo sudo ln -s /etc/nginx/sites-available/django-demo /etc/nginx/sites-enabled/ sudo systemctl enable django-demo sudo systemctl start django-demo sudo systemctl restart nginx
⚙️ Configuration
Environment Variables
Create a .env file based on .env.example:
# Required
SECRET_KEY=your-super-secret-key-here
DEBUG=False
ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
# Database (choose one method)
# Method 1: DATABASE_URL
DATABASE_URL=postgresql://username:password@hostname:port/database_name
# Method 2: Individual settings
USE_POSTGRES=True
DB_NAME=demo_db
DB_USER=demo_user
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
# Security
SECURE_SSL_REDIRECT=True # Set to True if using HTTPS
Database Options
- SQLite (Development): Default, no additional setup required
- PostgreSQL (Production): Set
USE_POSTGRES=Trueor provideDATABASE_URL
🔧 Management Commands
Development
python manage.py runserver # Start development server
python manage.py migrate # Apply database migrations
python manage.py createsuperuser # Create admin user
python manage.py collectstatic # Collect static files
Production
sudo systemctl restart django-demo # Restart Django service
sudo systemctl status django-demo # Check service status
sudo journalctl -u django-demo -f # View live logs
sudo bash deploy/update.sh # Deploy updates
📁 Project Structure
django-demo/
├── core/ # Main Django app
│ ├── templates/ # HTML templates
│ ├── static/ # CSS, JS files
│ ├── models.py # Database models
│ ├── views.py # View functions
│ ├── forms.py # Django forms
│ └── admin.py # Admin configuration
├── demo_project/ # Django project settings
│ ├── settings.py # Main settings
│ ├── urls.py # URL configuration
│ └── wsgi.py # WSGI application
├── deploy/ # Deployment files
│ ├── deploy.sh # Deployment script
│ ├── update.sh # Update script
│ ├── gunicorn.conf.py # Gunicorn configuration
│ ├── nginx.conf # Nginx configuration
│ └── systemd.service # Systemd service file
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── README.md # This file
🔍 Troubleshooting
Common Issues
-
Static files not loading:
python manage.py collectstatic --noinput sudo systemctl restart django-demo -
Database connection errors:
- Check
.envfile database settings - Ensure PostgreSQL is running:
sudo systemctl status postgresql
- Check
-
Permission errors:
sudo chown -R www-data:www-data /home/ubuntu/django-demo -
Service not starting:
sudo journalctl -u django-demo -f # Check logs sudo systemctl status django-demo # Check status
Log Files
- Django logs:
sudo journalctl -u django-demo -f - Nginx access:
/var/log/nginx/access.log - Nginx errors:
/var/log/nginx/error.log - Custom Django logs:
/var/log/django/
🔒 Security Considerations
- Change
SECRET_KEYin production - Set
DEBUG=Falsein production - Use HTTPS in production (
SECURE_SSL_REDIRECT=True) - Regularly update dependencies
- Configure firewall (UFW) properly
- Use strong passwords for database users
🚀 Deployment Checklist
- Update
.envwith production settings - Set
DEBUG=False - Configure proper
ALLOWED_HOSTS - Set strong
SECRET_KEY - Configure database settings
- Update domain in nginx configuration
- Set up SSL certificate (recommended)
- Configure firewall rules
- Test all functionality
📝 License
This project is open source and available under the MIT License.
🤝 Contributing
This is a demo project, but improvements are welcome! Please feel free to submit issues and pull requests.