Django Project Deployment Guide on
PythonAnywhere
This guide provides step-by-step instructions for deploying your Django student portal
project on PythonAnywhere, including setting up a MySQL database and performing
migrations. The instructions are tailored for the PythonAnywhere account
aapolystudentdata .
1. Prepare Your Project
Before uploading, ensure your Django project is ready for deployment. This includes:
• [Link] : Create a [Link] file in your project's root directory listing all
Python dependencies. You can generate this using pip freeze > [Link] in your
local virtual environment.
• [Link] : Configure your [Link] to handle production settings. It's
recommended to use environment variables for sensitive information like SECRET_KEY
and database credentials. For now, we will directly configure the database settings for
PythonAnywhere.
2. Upload Your Code to PythonAnywhere
PythonAnywhere provides several ways to upload your code. For a zipped project, the
recommended method is to upload it via the
"Files" tab and then extract it using the bash console.
1. Upload the ZIP file: Go to the "Files" tab on your PythonAnywhere dashboard. Navigate
to your desired project directory (e.g., /home/aapolystudentdata/ ). Click "Upload a file"
and select your project's .zip file.
2. Extract the project: Open a "Bash console" from your PythonAnywhere dashboard.
Navigate to the directory where you uploaded your zip file:
Bash
cd /home/aapolystudentdata/
unzip your_project_name.zip
Replace your_project_name.zip with the actual name of your zip file. This will extract your
project into a new directory, typically named after your project.
Recommendation: Rename the extracted folder to student_portal_django_project for
consistency.
Bash
mv extracted_folder_name student_portal_django_project
3. Set Up a Virtual Environment
It is crucial to set up a virtual environment for your project to manage dependencies. This
ensures your project has its own isolated set of Python packages.
1. Open a Bash Console: If you don't have one open, start a new "Bash console" from
your PythonAnywhere dashboard.
2. Create a virtual environment: Use the mkvirtualenv command to create a new virtual
environment. Choose the Python version that matches your project (e.g., Python 3.10).
Bash
mkvirtualenv --python=/usr/bin/python3.10 student_portal_env
This command creates a virtual environment named student_portal_env .
3. Install dependencies: Navigate into your project directory and install the
dependencies listed in your [Link] file.
Bash
cd /home/aapolystudentdata/student_portal_django_project
pip install -r [Link]
pip install mysqlclient
The mysqlclient package is necessary for Django to interact with MySQL databases.
4. Configure MySQL Database
PythonAnywhere provides a MySQL database for your projects. You'll need to configure it
and update your Django settings.
1. Set up MySQL on PythonAnywhere: Go to the "Databases" tab on your
PythonAnywhere dashboard. If you haven't already, set a password for your MySQL
account. Note down your MySQL username, hostname, and the full database name
(e.g., aapolystudentdata$your_database_name ). You can create a new database from this
tab if needed.
2. Update [Link] : Open your [Link] file within your Django project (e.g.,
/home/aapolystudentdata/student_portal_django_project/student_portal_django_project/settings.p
y ). Modify the DATABASES setting to connect to your PythonAnywhere MySQL database:
Python
DATABASES = {
'default': {
'ENGINE': '[Link]',
'NAME': 'aapolystudentdata$your_database_name', # Replace with your
'USER': 'aapolystudentdata',
'PASSWORD': 'your_mysql_password', # Replace with your MySQL passwor
'HOST': '[Link]',
'PORT': '',
}
}
Important: Replace your_database_name and your_mysql_password with your actual
MySQL database name and password from the PythonAnywhere "Databases" tab.
3. Run Migrations: In your bash console, navigate to your project's root directory (where
[Link] is located) and run the migrations:
Bash
cd /home/aapolystudentdata/student_portal_django_project
python [Link] makemigrations
python [Link] migrate
This will create the necessary database tables for your Django project in your MySQL
database.
5. Configure Your Web App
Now, you'll set up your web application on PythonAnywhere.
1. Create a Web App: Go to the "Web" tab on your PythonAnywhere dashboard. Click
"Add a new web app".
• Choose Manual Configuration (not the Django option).
• Select the same Python version you used for your virtual environment (e.g., Python
3.10).
2. Virtualenv: In the "Virtualenv" section, enter the path to your virtual environment:
Plain Text
/home/aapolystudentdata/.virtualenvs/student_portal_env
3. Code: In the "Code" section, set the following:
• Source code: /home/aapolystudentdata/student_portal_django_project
• Working directory: /home/aapolystudentdata/student_portal_django_project
4. WSGI Configuration: Click on the WSGI configuration file link (it will look something
like /var/www/aapolystudentdata_pythonanywhere_com_wsgi.py ). Edit the file to look like this:
Python
import os
import sys
path = '/home/aapolystudentdata/student_portal_django_project'
if path not in [Link]:
[Link](0, path)
[Link]['DJANGO_SETTINGS_MODULE'] = 'student_portal_django_project.settin
from [Link] import get_wsgi_application
application = get_wsgi_application()
Ensure the path variable points to your project's root directory and
DJANGO_SETTINGS_MODULE correctly points to your [Link] file (e.g.,
your_project_name.settings ).
5. Static Files: Configure static files in the "Web" tab. This tells PythonAnywhere where to
find your static assets (CSS, JavaScript, images).
• Under "Static files", add a new entry:
• URL: /static/
• Directory: /home/aapolystudentdata/student_portal_django_project/static
• If your project uses media files (user-uploaded content), add another entry:
• URL: /media/
• Directory: /home/aapolystudentdata/student_portal_django_project/media
6. Collect Static Files: In your bash console, navigate to your project's root directory and
run:
Bash
cd /home/aapolystudentdata/student_portal_django_project
python [Link] collectstatic
This will gather all static files into the directory specified in your STATIC_ROOT setting
(which should be /home/aapolystudentdata/student_portal_django_project/static if you
followed the above).
7. Reload Web App: Go back to the "Web" tab and click the "Reload" button for your web
app.
Your Django student portal should now be live on PythonAnywhere! If you encounter any
issues, check the error logs linked on the "Web" tab for debugging information.