Django in Pythonanywhere
At the beginning of my adventure with Django, I had a strong desire to deploy my project somewhere so that I could see the effect of my work. Like every other human being, I am more motivated to work if I see my progress & improvements once in a while. Pythonanywhere really helped me accomplish this. Maybe you haven’t heard the name of this page so let me explain what this page is about. Even though it helps you to deploy your page in Flash, Django, etc., at the same time you can create Jupyter notebooks and automate some tasks with the scheduling function. You can create a script, which is scrapping the web and alerting you if the change you expected happened. Hence, it is quite useful. I am also using the scheduling function in my Django project since I need to update my database depending on variables every day and I wouldn’t like to worry about it and do manually. You can also create always-on tasks that are always running on the server. So briefly explaining, Pythonanywhere is a company giving hosting services but at the same time an online integrated development environment, and it might help you a lot if you like programming in Python.
But the main topic of this article is to explain how to deploy your Django project in Pythonanywhere so let’s begin with it. When someone doesn’t know how the system works, it seems complicated but Pythonanywhere really takes most of the complexities from your hand and you need to do only a few things to be able to serve your page online.
Setting Up Your Virtual Environment
When you start your project, please make sure that you set up your virtual environment to have full control over the system running on the server. It is really helpful when there is a release for a package that you are using and it is updated in the server even though you wrote your code using the old version. Also, somethings might change and you might be surprised when you come back to your project. For example, I wrote my Django project using 3.0.8 version, however, once the package was updated to 3.1.3, I realized that somethings are broken on my admin page. So I needed to remove Django and install the old version in my virtual environment to come back to normal.
To set up a virtual environment, you need to first navigate to the folder where you want to save using the cd command in your terminal and install giving a specific name you want to call:
virtualenv -p python3 <Desired-Directory-Name>
So let’s say that we called our virtual environment “ django3”. Now you need to activate the virtual environment and install Django:
pip install Django
Or maybe you already have the requirements.txt file that you created from another virtual environment, you can simply install them using the following command:
pip install -r requirements.txt
Getting Your Django Project Using Git
You can absolutely start a project from scratch on Pythonanywhere, but my personal recommendations is setting up all necessary things offline and then bring them to production with the help of Git. At this point I assume that you have created your repository in Github (or any other provider) and committed & pushed all recent changes.
git clone <Your Git Repository Link (ie.https://github.com/ozntel/django3)>
python manage.py check
python manage.py makemigrations
python manage.py migrate
Please make sure to use your own directory names since I provided as an example above. We accomplished to clone our code from the GitHub repository, checked the status if there is an error, and complete migrations to make sure that everything is clean in our database.
Here comes the juicy part. We need to make small changes so that our project runs safely in the server. Firstly, go to your project and make the change below within settings.py:
ALLOWED_HOSTS = [ '*' ]
This will allow you to run your Django project in any hosting environment. Absolutely, it is not safe to do but it is easier for beginners. If you already know your URL, you can provide directly:
ALLOWED_HOSTS = [ 'ozan.pythonanywhere.com', '127.0.0.1' ]
I included 127.0.0.1 so that we can also run the project on our local server.
Now you need to navigate to the ‘ Web’ tab on the Pythonanywhere page and set up your project settings. Firstly, you need to amend the source code and working directory. Please make sure that these indicate the main project folder and not any application.
Source Code : /home/ozan/django3
Working Directory : /home/ozan/django3
You need to now go to the “ wsgi.py” file under the working director and change the codes as below. Make sure that you change “ PROJECT_NAME”:
import sys path = os.path.expanduser('~/<PROJECT_NAME>')
if path not in sys.path:
os.environ['DJANGO_SETTINGS_MODULE'] = '<PROJECT_NAME>.settings' from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler application = StaticFilesHandler(get_wsgi_application())
Now you need to set up your virtual environment. Within the same “ Web” tab, you will see “ Virtualenv” settings below. You need to indicate the directory where you set up the Virtual environment from the first step. Also, you should make sure that you are indicating the main folder of the environment you set up and not the activation as you do in the terminal:
Additionally, as my personal recommendation, you can select “ Force HTTPS” below. This will help all users who enter your unsecure site (HTTP) to be directed to the secure site (HTTPS). Also, you might want to select the HTTPS certificate as “ Auto-renewing Let’s Encrypt certificate”.
That’s all. Now the only thing you need to do is to scroll up in the “ Web” tab and click “ Reload” your page. You need to remember that changes you make will not be visible on your page unless your Reload your page.
Hopefully, this guide will help you to set up your Django project in Pythonanywhere. To be honest, I initially created my web page in WordPress, but I decided to move all my content into Django and setup in Pythonanywhere once I saw how this was easy for me. Absolutely, you need to pay 5 USD to be able to change your domain name and use a custom one, which I do, but in case you don’t want to pay, you can always deploy your page with the domain name Pythonanywhere is giving to you and this is totally free.
In case you have any suggestions or you encounter any problem during deployment, please let me know. I will be happy to help you. I was also a beginner and I know how it might be frustrating when you do everything by book but it still doesn’t work.
Originally published at https://www.ozan.pl.