Introduction
In this final part, we'll walk through deploying our Django Project Budget Manager to PythonAnywhere, a popular platform for hosting Python web applications. We'll cover each step in detail to ensure a smooth deployment process.
Prerequisites
-
Create a PythonAnywhere account:
- Visit www.pythonanywhere.com
- Sign up for a free account (or choose a paid plan for more features)
Prepare your project:
# Create requirements.txt
pip freeze > requirements.txt
# Create runtime.txt (specify Python version)
echo "python-3.9" > runtime.txt
Step 1: Configure Production Settings
Create a production settings file:
# config/settings/production.py
from .base import *
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# Allow only your PythonAnywhere domain
ALLOWED_HOSTS = ['yourusername.pythonanywhere.com']
# Security settings
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
# Static files configuration
STATIC_ROOT = '/home/yourusername/project_budget/staticfiles'
MEDIA_ROOT = '/home/yourusername/project_budget/media'
# Database configuration (using MySQL on PythonAnywhere)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourusername$project_budget',
'USER': 'yourusername',
'PASSWORD': 'your-mysql-password',
'HOST': 'yourusername.mysql.pythonanywhere-services.com',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
# Email configuration (using SMTP)
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@gmail.com'
EMAIL_HOST_PASSWORD = 'your-app-specific-password'
Step 2: Upload Project to PythonAnywhere
- Open a Bash console on PythonAnywhere and clone your repository:
# Navigate to home directory
cd ~
# Clone your repository
git clone https://github.com/yourusername/project_budget.git
# Create virtual environment
mkvirtualenv --python=/usr/bin/python3.9 project_budget
# Activate virtual environment
workon project_budget
# Install dependencies
pip install -r project_budget/requirements.txt
- Create necessary directories:
mkdir ~/project_budget/staticfiles
mkdir ~/project_budget/media
Step 3: Configure PythonAnywhere Web App
Go to the Web tab in PythonAnywhere dashboard
-
Click "Add a new web app" and choose:
- Your domain name (e.g., yourusername.pythonanywhere.com)
- Python version (3.9)
- Manual configuration
-
Configure the virtual environment:
- Virtual environment path:
/home/yourusername/.virtualenvs/project_budget
- Virtual environment path:
Configure WSGI file (
/var/www/yourusername_pythonanywhere_com_wsgi.py
):
import os
import sys
# Add project directory to Python path
path = '/home/yourusername/project_budget'
if path not in sys.path:
sys.path.append(path)
# Set environment variables
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.production'
os.environ['SECRET_KEY'] = 'your-secret-key'
# Initialize Django
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
- Configure static files in the Web tab:
- URL:
/static/
- Directory:
/home/yourusername/project_budget/staticfiles
- URL:
- URL:
/media/
- Directory:
/home/yourusername/project_budget/media
Step 4: Set Up Database
- Go to the Databases tab and create a MySQL database:
CREATE DATABASE yourusername$project_budget CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Set a password for your MySQL database
Apply migrations:
cd ~/project_budget
python manage.py migrate
- Create a superuser:
python manage.py createsuperuser
Step 5: Collect Static Files
python manage.py collectstatic
Step 6: Final Configuration
-
Set up environment variables in the Web tab:
- Click on the "Environment variables" section
- Add the following variables:
DJANGO_SETTINGS_MODULE=config.settings.production SECRET_KEY=your-secret-key DATABASE_URL=mysql://yourusername:password@yourusername.mysql.pythonanywhere-services.com/yourusername$project_budget
-
Enable HTTPS:
- In the Web tab, enable "Force HTTPS"
Reload your web app
Testing the Deployment
Visit your site at
https://yourusername.pythonanywhere.com
-
Test the following:
- User registration and login
- Project creation and management
- File uploads
- Email functionality
Common Issues and Solutions
-
Static files not loading
- Check STATIC_ROOT and MEDIA_ROOT paths
- Run
collectstatic
again - Verify static files configuration in Web tab
-
Database connection errors
- Verify database credentials
- Check database configuration in settings
- Ensure MySQL server is running
-
500 Server Error
- Check error logs in the Web tab
- Verify WSGI file configuration
- Check permissions on media and static directories
-
Email not working
- Verify email settings
- Check if your email provider allows SMTP
- Use app-specific password for Gmail
Maintenance Tasks
- Updating the application:
# Pull latest changes
cd ~/project_budget
git pull
# Activate virtual environment
workon project_budget
# Install new dependencies
pip install -r requirements.txt
# Apply migrations
python manage.py migrate
# Collect static files
python manage.py collectstatic
# Reload web app from PythonAnywhere dashboard
- Backing up data:
# Backup database
python manage.py dumpdata > backup.json
# Backup media files
zip -r media_backup.zip media/
Security Considerations
-
Keep secrets secure:
- Never commit sensitive data to version control
- Use environment variables for secrets
- Regularly rotate passwords and keys
-
Regular updates:
- Keep Django and other packages updated
- Monitor security advisories
- Apply security patches promptly
-
Monitor logs:
- Check access logs regularly
- Monitor error logs
- Set up alerts for suspicious activity
Additional Resources
Remember to regularly backup your database and media files, and keep your application and dependencies updated with security patches.
This article is part of the "Building a Project Budget Manager with Django" series. Check out Part 1, Part 2,Part 3, Part 4, Part 5,and Part 6 if you haven't already!
Getting Help
If you encounter issues during deployment:
- Check the PythonAnywhere error logs
- Visit the PythonAnywhere forums
- Review Django deployment documentation
- Join Django communities for support:
- Django Forum
- Django Discord
- Stack Overflow with tags: [django] and [pythonanywhere]
Top comments (0)