Deploy a Django App on PythonAnywhere

Posted: September 08, 2024 | Updated: October 06, 2024

If you are learning Django and you want to host your Django app online, you need a perfect web hosting solution. There are lot of online services that offer Python hosting solutions, but when hosting your app for the first time, you need an easy-to-use solution. PythonAnywhere is one of the great option for newbies.

Pythonanywhere

PythonAnywhere offers both free and paid hosting solutions for Python based apps . The free version only allows you to host your app on a PythonAnywhere based subdomain. If you purchase a paid version (starting at $5 per month), you can set up one main domain and use a MySQL database. I advise choosing MySQL or PostgreSQL as the database for your Python app. Using SQLite on PythonAnywhere can affect your site's speed.

If you switch to a custom plan, you can use both MySQL and PostgreSQL databases. The custom plan also allows you to add several main domains, adjust file storage size, and increase PostgreSQL database size. You can set up the payment settings as a monthly recurring payment from your credit card. If you host two domains with a PostgreSQL database, it will cost approximately $14.

Once you know how to host Django apps online, you can easily switch to another hosting solution. Therefore, I highly recommend PythonAnywhere as your first Python hosting provider, as it helps you save a lot of time you would otherwise spend on hosting setup.

So, how do you host your Django app on PythonAnywhere?

Step 1: First, create a PythonAnywhere account. Choose 'Beginner account' if you plan to add a test Django app in a PythonAnywhere subdomain. Choose 'Hacker account' if you want to host your Django app using a custom domain name.

Step 2: Then create a public or private repository on GitHub and add your Django app code to that repository.

Step 3: Create a virtual environment in PythonAnywhere using the Bash console, and clone your GitHub repository into PythonAnywhere. Activate the virtual environment and install the required Python packages using the requirements.txt file.

To do that, open bash console in Pythonanywhere and type the following command in bash

git clone [email protected]:gitusername/appname.git

Then create a virtual environment using the following command

mkvirtualenv venv

Note: Here "venv" is a virtual environment name. use venv1, and venv2 like names in the future for different Python apps if you going to add in PythonAnywhere. 

Use the following command to see the project working directory

pwd

To see the all files listed in the working directory, use the following command

ls

it will list the files and directories like this

README.txt appname

To navigate the folder, use the following command

cd appname

Now install the Django and required packages either from requirements.txt file or using the pip command

pip install -r requirements.txt

Step 4: Add a New App in PythonAnywhere

In Web Apps, choose  "Add a new Web App"

In the "Select Python Web Framework" option,  choose "Manual configuration (including virtualenvs)

Select the latest Python version and choose Next.

First, add the virtual environment settings (Just, add the virtual environment name ex : venv )

Next In the code section, open the "WSGI configuration file"

Remove all codes except "+++++++++++ DJANGO +++++++++++"

And uncomment all single "#" comments.

Change the path to your Django folder name in Pythonanywhere

Also, add your main project name in os.environ['DJANGO_SETTINGS_MODULE']

Save the file and reload the site.

Now in your PythonAnywhere, click the "Files" Menu.

Go to your project folder and open "settings.py".

In ALLOWED_HOSTS, add your pythonanywhere subdomain without HTTP and www.

ALLOWED_HOSTS = ['username.pythonanywhere.com']

Plain textCopyMore options

Also change the DEBUG = True to FALSE, if you launch your site to the production version.

Save the file and reload the site.

Note: Don't forget to set the allowed host in settings.py file

ALLOWED_HOSTS = ['username.pythonanywhere.com']

Watch this video 'Deploy a Django Web App on PythonAnywhere." to see more info about How to Deploy a Django App on PythonAnywhere.

© 2024 Webapptiv. All rights reserved.