How To Create And Run A Web App On Pythonanywhere

What is pythonanywhere

Python anywhere is online hosting platform just like heroku, aws and C-panels platform. But the difference is that pythonanywhere have online console where you can run and maintain your app like your local computer.

To know more about pythonanywhere read our post below:

Just like the way you upload your existing app on pythonanywhere that is how apps are being created on pythonanywhere.

Note about pythonanywhere

  • Pythonanywhere only Host python apps
  • Pythonanywhere offers free ssl certificate
  • pythonanywhere is free except for custom domain
  • pythonanywhere plans are cheap compare to other hosting apps; Their plans ranges from $3 – $15 plus more depending on how you customize it.
  • Pythonanywhere renew ssl certifacate every month to make it more secure.
  • pythonanywhere is part of ANACONDA

Steps involve in creating or starting a django web app on pythonanywhere

First step:

I assume that you have created account onPythonanywhere

Secondly:
After creating a begineer account which is free, another thing to do is to create a virtual environment just like what we do in Vs Code

Follow this steps to create a virtual environment

1. Run the code below, it will create a virtual environment with name venv

$ mkvirtualenv --python=/usr/bin/python3.8 venv

The first command here is command for creating a virtual environment, secondly –python=usr/bin ia the path to be configured then thirdly python version is added too, then name of the evironment you have created.

Upon succesful creation of the environment the virtual environment activates by itself, if yours is succesfully created . Incase if it doesn’t, activate it by runnng workon venv.

Next you have to install django by running pip install django, also make migrations; Check the codes below.

#Run this code on your console

pip install django
# you have to start a project and naming it whatever you want 
django-admin startproject mysite
#you have to create an app
cd mysite
 ./manage.py startapp myapp
# here I have named it myapp

#after that run migrations

./manage.py makemigrations

./manage.py migrate

Then head over to web app by pressing the right button on the right top of the menu.

Then select webapp,

Click add web app then select manual configuration, after that select the python version that you have created before. remember that we have selected python 3.8 while creating the virtual environment therefore you would have to select python 3.8 then click next.

Now you have created your web app it time to configure your static files. run this command pip install whitenoise.

Why am I installing whitenoise? Whitenoise handles static files in a well orgainised way. If you don’t want to configure more things on pythonanywhere then install whitenoise

pip install whitenoise

Make sure your virtual environment is running before installing it then add it’s middleware to your middleware configuration.

head over to settings .py then open it then add this middleware in your middleware configuration.

MIDDLEWARE = [
    # ...
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",#here
    # ...
]

Then add your domain to the allowed host e.g allowed_host = [‘9japlus.com’]
You don’t need to add protocols in when adding your domain, check the code snippet below


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['yourusername.pythonanywhere.com']
#If you have a custom domain and you like to include it then add it like this.
ALLOWED_HOSTS = ['customdomain.com']

If you have a custom domain and you want to include it then make sure you have configured it with your domain host.

Check this post for configuring pythonanywhere with domain host

Now lets configure our app go to your web app at the right corner, Then on these place enter the part of your webapp

source code
Code:
What your site is running.

Source code:
'Enter the path to your web app source code'

For this post my own is running at

What your site <strong>is</strong> running.

Source code:
/home/Mytuto/pytutorial/myapp

Go to directory

How to get your own source code, go to files then enter the page hosting your application then enter the folder hosting your webapp which you have created before then copy it.example is below

source

then go down you will have to enter path to your virtual environment, just enter the name of the virtual environment then the system will complete it for you


/home/Mytuto/.virtualenvs/venv

Now for the staticfiles the whitenoise you installed will be taking care of your static files but you can still add it manually ,for this tutorial, this is how i configured it.Go down to static file name space then enter the path like the example below

virtual
URLDirectory
/static//home/Mytuto/pytutorial/staticfiles

We are all set, now we have to configure our wsgi files

wsgi

Delete every thing and except the code below.

The folder that contains settings.py you have to add to the path like my own below also add it to the os.environ line, like my own below

 +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/Mytuto/mysite/mysite/settings.py'
## and your manage.py is is at '/home/Mytuto/mysite/manage.py'
path = '/home/Mytuto/pytutorial'
if path not in sys.path:
    sys.path.append(path)
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'pytutorial.settings'
#
## then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
django image

Then save it, and head over to web app then reload the web app at your domain.com.

reload

After reloading it, head over to your username.pythonanywhere.com picture above; then refresh it then you will see django website with congratulations.

This is just a step in which you can run django on pythonanywhere,follow the step in Vs code or any other text editor to create a dynamic website .

Incase you are new to django or you do not follow how we got up to here, you can leave a message below.

Leave a Reply

Your email address will not be published. Required fields are marked *