Since Django framework has a special syntax and structure, it’s important to make sure that a chosen provider makes it possible to easily deploy and scale any Django project.
There are many Django-compatible hosting providers which are worth considering. This post documents my experience deploying a basic Python based web app in " PythonAnywhere". PythonAnywhere runs on powerful Amazon EC2 servers and it is pre-configured with tons of libraries.
Steps:
Create a free user account in GitHub.com (If you haven't done it yet!)
Then, create a new repository, giving it the name. My test project was named "DjangoTest".
Set up our Django Test App on PythonAnywhere
- Sign up for a PythonAnywhere account in www.pythonanywhere.com PythonAnywhere is a service for hosting Python code on servers "in the cloud". Sign up for a "Beginner" account on PythonAnywhere (You don't need a credit card).
-
Note:
When choosing your username here, remember that your blog's URL will be in the form
"yourusername.pythonanywhere.
com". So choose an appropriate name (e.g. for what your web app is all about).
Create a PythonAnywhere API token
This is something you need to do only once. Sign in to your
"PythonAnywhere" account and
go to your dashboard. Find the link near the top right to your "Account" page and then select
the tab named "API token". Click on "Create new API token" button.
Configure web app on PythonAnywhere
- Go back to the main PythonAnywhere Dashboard and choose the option to start a "Bash" console(which is a PythonAnywhere version of a command line)
- Deploying a web app on PythonAnywhere involves pulling down your code from GitHub, and then configuring PythonAnywhere to recognize it and start serving it as a web application. There are manual ways of doing it, but PythonAnywhere provides a helper tool that will do it all for you. Let's install it first:
Note: Remember to run the below command in PythonAnywhere command-line and not
in your PC, otherwise cloning with fail.
$ pip3.6 install --user pythonanywhere
That should print out some things like Collecting pythonanywhere, and
eventually end
with a line saying Successfully installed (...)
pythonanywhere- (...).
- Now we can run the helper to automatically configure our app from GitHub.
place of <github-username>):
$ pa_autoconfigure_django.py
https://github.com/<github-username>/DjangoTest. git
As you watch that running, you'll be able to see what it's doing:
- Downloading your code from GitHub
- Creating a virtualenv on PythonAnywhere, just like the one on your own computer
- Updating your settings file with some deployment settings
- Setting up a database on PythonAnywhere using the manage.py migrate command
- Setting up your static files (we'll learn about these later)
- And configuring PythonAnywhere to serve your web app via its API
On PythonAnywhere all those steps are automated,
but they're the same steps you would
have to go through with any other server provider.
Initialize the admin account with a super user
PythonAnywhere
has automatically activated your virtualenv for you, so all you need to
do is
run the below command in PythonAnywhere command-line
(user_name.pythonanywhere.com )
$ python manage.py createsuperuser
Type in the details (username, email and password )for your admin user.Now, you can also
take a look at your code on PythonAnywhere using ls command in PythonAnywhere
command-line:
(user_name.pythonanywhere.com)
$ ls
You are now live!
Your site should now be live on the public Internet! Click through to the PythonAnywhere
"Web" page to get a link to it.
Comments