Deploying a Django Application ON "pythonanywhere".
Published: 2024-05-27 08:25:06


Table Of Content
- Introduction
- Setting up Django Project Locally
- GitHub Upload Guide
- Deploying Project on PythonAnywhere Guide
- Conclusion
- References


Introduction

“Pythonanywhere” is a popular cloud-based platform that allows developers to host Python applications effortlessly. It provides a user-friendly environment for deploying Django applications, making it a preferred choice for both beginners and seasoned developers. This document is a step by step guide on how to deploy django application on "pythonanywhere".


Step 1: Set up your Django Project (Local Changes)


1. Create a Projects Directory

Create a directory to keep your Django projects organized and navigate into it.


    mkdir -p ~/Desktop/Projects
    cd ~/Desktop/Projects


I have created Projects Folder in Desktop and run the commands above, you should see something like:

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716710210378_image.png)




2. Set Up and Activate a Virtual Environment

Create a virtual environment to isolate your project's dependencies.


    python3 -m venv env
    source env/bin/activate

You will see (env) at the beginning, this ensures that you are on activated virtual environment. 

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716710497377_image.png)




3. Install Django

With the virtual environment activated, install Django.


    pip install django

 
 Wait until the installation is complete:
 

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716711034123_image.png)




4. Create a New Django Project

Now, create your Django project named "deploy_on_pythonanywhere".


    django-admin startproject deploy_on_pythonanywhere

if you encounter an error similar to:

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716711341591_image.png)


Don’t panic, run the command below starting with the path where you installed django:


     ~/Desktop/Projects/env/bin/django-admin startproject deploy_on_pythonanywhere




5. Navigate into the Project Directory

Change the directory to the newly created project.


    cd deploy_on_pythonanywhere

6. Create a New App within the Project
Create a new app named "deploy_on_anywhere".


    python manage.py startapp deploy_on_anywhere



7. Generate `requirements.txt` File

Generate the `requirements.txt` file to list all the dependencies.

Ensure you are on same path as manage.py before running the command below:


    pip freeze > requirements.txt


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716711889270_image.png)


Finally, Open your locally created Project on editor of your Choice, I will go with Visual Studio Code.

The file structure of your project should look like:

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716712046607_image.png)





  8.  Configure “deploy_on_anywhere” app in your project settings.

  Navigate to:

    deploy_on_pythonanywhere/settings.py


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716714291341_image.png)




9. Configures the URLs in your project settings.

  Navigate to:

    deploy_on_pythonanywhere/url.py


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716714344805_image.png)




10.  Set up the app’s URL configuration for your app:

Navigate to urls.py or create one if it doesn’t exist:


    deploy_on_anywhere/urls.py
![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716715408147_image.png)





11. Create your first view for the app:

      Views are a critical component that handles the logic for processing user requests and returning appropriate responses.
      
Navigate to:


    deploy_on_anywhere/views


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716715555310_image.png)




12.  Apply database migrations:

  Database migrations in Django are a way to propagate changes you make to your models (like adding a field or changing a field type) into your database schema.

Run the following command on the integrated terminal:



      python manage.py migrate



![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716715080975_image.png)


       
       
       

 13.  Run the development server:

 

      python manage.py runserver


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716715147096_image.png)


       

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716715310200_image.png)


STEP 2 : UPLOADING PROJECT ON GITHUB.

Create new Repository on your Github account, name it `deploy_on_anywhere`

You can choose either Private or Public repo, I will choose public for everyone to view my project, I will attach the link to the project in reference.

Save the changes.

Back on your IDE integrated terminal, run the commands below; 

(Note: you must have `git` installed on your machine and also globally configure it. refer to {Link to git setup documentation on reference}.


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716716438467_image.png)



    git init
    git add README.md
    git remote add origin git@github.com:DevNathanHub/deploy_on_anywhere.git
    git add .
    git commit -m "first commit for deploying django on pythonanywhere"
    git branch -M main
    git push -u origin main


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716716594102_image.png)

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716716640962_image.png)



Finally, Refresh your github after few minutes and you will see the changes updated, 

![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716716796802_image.png)




 Step 3: DEPLOYING PROJECT ON "Pythonanywhere"
1. Create an account on "Pythonanywhere".


2. After registering you can see the page dashboard below:
![](https://paper-attachments.dropboxusercontent.com/s_B05363D02E316568BF012F09D8D14ED0C69EF06704D2B2A88E0EDC6AC6C4EEC7_1716460398875_file.jpeg)


On the dashboard page on the top right next to Dashboard click Console and select Bash.

Clone your own project, copy the link from your github repository;


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716717213315_image.png)


Then run the command on bash;

    git clone [paste the copied link]


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716717782284_image.png)


Create and setup environment variables and copy the path of your directories which you installed on bash similar to how you did locally. The commands and their respective outputs are shown below:


![](https://paper-attachments.dropboxusercontent.com/s_95967AB41E3CC65BD756BA1475CF51D7A5FE329FC67E49DE335D3CB4D115BD11_1716718186694_image.png)


Navigate to your project directory and install requirements using the commands below:


    cd deploy_on_anywhere
    pip install -r requirements.txt

On the dashboard page, top right click on the Web option and select " Add a new web app".


![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716456983956_web+app.png)


Click on next and follow the procedure
Select Django as the framework:


![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716457671805_selectdjango.png)


 
Select the python version to use. In this case, we are using Python version 3.10

![](https://paper-attachments.dropboxusercontent.com/s_B05363D02E316568BF012F09D8D14ED0C69EF06704D2B2A88E0EDC6AC6C4EEC7_1716792601927_file.jpeg)


Open the WSGI Configuration file under the web section.
  
  

![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716457830555_Screenshot+2024-05-22+153513.png)


Edit WSGI configuration file on line no. 12 and 17 replace the word mysite with your project name which you cloned from GitHub.


![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716458059131_Screenshot+2024-05-22+153622.png)



Now it looks like this and then click on save:

![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716458138695_Screenshot+2024-05-22+153809.png)


Select Virtualenv section under Web:


![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716458209069_virtualenv.png)


Enter the path of Virtualenv as we created using bash (refer above pwd command for path)


![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716458398037_virtualenv101.png)


Click on Reload under the Web section and visit the link.


![](https://paper-attachments.dropboxusercontent.com/s_2FBE028B00EC2B60169B9C060C9999DB59A80B6CA0F5DC3A82442BF470923C4F_1716458571467_finaldeployment101.png)



Conclusion

By following this comprehensive guide, you should now have a fully functioning Django project set up and running on "pythonanywhere.com".


References:
1. How to Install git on; 
- Linux, Windows and macOs. 



2. Startup Github Repo Click Here.


3. Create Account on Python Anywhere 


4. YouTube Video Reference on How to Deploy Django Project Watch Tutorial. 



https://www.geeksforgeeks.org/how-to-deploy-django-project-on-pythonanywhere/