How To Host Django Website On Pythonanywhere

Developing a website and host Django website is not an easy one; after developing a website with any MCS what will come to your mind is how to make it visible to the whole wide world. Yes without hosting it, It is only on your local computer and visible to you alone.



TABLE OF CONTENTS

  • What is Web Hosting?
  • What is Django framework?
  • What is Pythonanywhere?
  • Why I recommend Pythonanywhere to absolute beginners
  • Steps involve in Hosting your app on pythonanywhere

Before we continue let me explain what is hosting, django and pythonanywhere.

What is hosting or Website Hosting
A web hosting service is a type of Internet hosting service that hosts websites for clients, i.e. it offers the facilities required for them to create and maintain a site and makes it accessible on the World Wide Web. Companies providing web hosting services are sometimes called web hosts.

There are many hosting companies out there; In this post I will mention few.

pythonanywhere

Some of web hosting are:

What is django

Django is apython frameworkfor web development.

What is Django? Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

What is Pythonanywhere
PythonAnywhereis an online integrated development environment and web hosting service based on the Python programming language. Founded by Giles Thomas and Robert Smithson in 2012, it provides in-browser access to server-based Python and Bash command-line interfaces, along with a code editor with syntax highlighting.

Why I recommended Pythonanywhere for absolute beginners
I recommend pythonanywhere to every beginners because

  • During development there might still be bugs in your code,Without redeploying you can log in to pythonanywhere and edit it like you are on vscode or local computer
  • Easy to deploy and no much installation
  • Very easy to configure at startup
  • Python anywhere have online console where you can practice and code like your local computer
  • Pythonanywhere can be access on any browser that supports css and javaScript
  • Pythonanywhere is mobile friendly when accessing it from Android
  • pythonanywhere allows you to store your images on Server
  • you can create multiple Virtual environment.
  • You can schedule a task to run for you when you are not even online

With the very points I have outlined above, You must be convinced of Hosting your app on pythonanywhere, then this post is for you.

Steps involve in Hosting your app on pythonanywhere:

  • Make sure that your app is ready and no bugs in it.
  • Install whitenoise
  • Add whitenoise middleware to your middleware.
  • Create a github account: I recommend github deployment you can also use other methods approprate to you
  • Add your domain name to allowed host

For this post I have created an app and deployed it to github, in case you don’t know how to deploy an app to github read this post.

How to commit files/project to github

While creating a repo on github make sure name of your project matches the name of your repo

Now I assumed that you have succesfully deploy your app on github then it’s time to clone the app.

Open console and click on bash then run git clonehttps://github.com/yourusername/repositoryname.git

~$git clone https://github.com/bennysjessie/pytutorial.git

Then create a virtual environment using this code ; mkvirtualenv –python=/usr/bin/python3.8 your virtual environment name

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

The virtual environment activates by itself if you succesfully created it.

Then run cd mysite, Next you have to install django by running pip install django, also make migrations.

#Run this code on your console
pip install django
#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. 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

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",
    # ...
]

Then add your domain to the allowed host e.g allowed_host = [‘9japlus.com’]


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

ALLOWED_HOSTS = ['yourdomain.com']

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

Code:
What your site <strong>is</strong> 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

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

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

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

Delete every thing and except this ,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:
<strong>import</strong> os
<strong>import</strong> 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'
<strong>if</strong> path <strong>not</strong> <strong>in</strong> sys.path:
    sys.path.append(path)
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'pytutorial.settings'
#
## then:
<strong>from</strong> django.core.wsgi <strong>import</strong> get_wsgi_application
application = get_wsgi_application()

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

Django

Leave a Reply

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