Deploy Django website for free with Python anywhere and GitHub

Time:2020-10-15

Before deploying the Django website, you should first handle the preliminary work, such as settings.py File settings to prevent the disclosure of private information. This article is just a simple notehttps://tutorial.djangogirls….

1. Publish Django project to GitHub

2. Apply for a free Python anywhere account
website:https://www.pythonanywhere.com/
Free account can only build one station. Station name can only be XXX.pythonanywhere.com 。 Where XXX represents the user name used to register the account. The site can only last for 2 months and will be deleted after 2 months.

3. Send the project on GitHub to Python anywhere
3.1 in Python anywhere, click “console” – > start a “bash” console
This Bash is similar to the “terminal” in Linux system.
3.2 in the newly created bash, enter:
Note: the following code indicates that it needs to be executed in bash of Python anywhere!!

$ git clone https://github.com/<your-github-username>/my-first-blog.git

This step sends GitHub’s project to Python anywhere. Note that the link above is the repository address of the project in GitHub:
Deploy Django website for free with Python anywhere and GitHub

Bash’s response is very slow and requires patience.
After sending, you can use the following code to see the file directory structure copied to Python anywhere. It should be emphasized that this my first blog is the project name of the repository in GitHub.
Note: the following code indicates that it needs to be executed in bash of Python anywhere!!

tree my-first-blog

4. Create a virtual environment for the project in Python anywhere
Continue to execute the following command in bash of Python anywhere to create a virtual environment named myvenv.
Note: the following code indicates that it needs to be executed in bash of Python anywhere!!

$ cd my-first-blog
$ virtualenv --python=python3.5 myvenv
$ source myvenv/bin/activate
(myvenv) $ pip install django==1.10.0

The Python and Django version numbers above are for reference only!

5. Create database and super account for projects in Python anywhere
Note: the following code indicates that it needs to be executed in bash of Python anywhere!!

(myvenv) $ python manage.py migrate
(myvenv) $ python manage.py createsuperuser

6. Publish the Django project as a web app
At this point, our Django project is already on Python anywhere, the virtual environment is ready, and the database is created. At this time, we can publish it as a web app! In Python anywhere, click the web tab and click add a new web app
->Confirm domain name > select manual configuration (note not “Django”) – > select Python 3.5 > click next to finish the wizard.

7. Configure the virtual environment of web app
After you create the virtual web environment, click the virtual where app, as shown in the following figure:

Deploy Django website for free with Python anywhere and GitHub

Change the address to:

/Home / < your account name of Python anywhere > / my first blog / myvenv

Note: the my first blog above is the repository address of the project in GitHub.
Note: myvenv above is the name of the virtual environment to be created

8. Configure the WSGI file in Python anywhere
Still in the web tab, click the blue font connection after the WSGI configuration file (as shown in the figure below).
Python anywhere will take us to a new page (a code editor) and replace all the editor’s code with the following code.

import os
import sys

Path '/ home / < your Python anywhere user name > / my first blog' ා use your own user name and GitHub project name here.
if path not in sys.path:
    sys.path.append(path)

os.environ ['DJANGO_ SETTINGS_ MODULE'] = ' mysite.settings Please use the name of the folder where the settings are located for MySite. I use the same name as the GitHub warehouse name
from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())

Click “save” – > return to the web tab and click the big green button “reload”. At this point, we can use the address provided on the web tab (i.e XXX.pythonanywhere.com )Yes! In this way, our website really exists on the Internet, although it is only valid for two months:-(

9, Debug
In the process of configuration, errors will inevitably lead to the site can not be published successfully. First of all:

  • View the error log displayed on the error page

  • Check the web tab page for any error prompts
    Common errors include:

  • Bash in Python anywhere misses creating virtualenv, activating it, installing Django for it, and initializing the database for it

  • There was an error when setting the path of virtual environment in step 7

  • There is an error in configuring WSGI in step 8. Have you filled in the correct path?

  • Is the python version selected when installing the virtual environment and the python selected when creating the web app are both the same version?

10. How to feed back the project modification in GitHub to Python anywhere?
When the project needs to be modified, it is usually modified on the local computer. After the modification, it is published to GitHub, and then to Python anywhere. After the project in GitHub is modified, enter the following two lines of code in bash of Python anywhere to synchronize:
Note: the following code indicates that it needs to be executed in bash of Python anywhere!!

cd my-first-blog
git pull