Feb. 14, 2020, 10:40 a.m.

Deploying Django App to PythonAnywhere

I have my Django project: https://gitlab.com/gian.mapacpac/communilytics
Based on the Django Girls Tutorial, we can use PythonAnywhere to host our project for free. Here is what I did, just repeating the steps in the tutorial.

Create a PythonAnywhere account, note that your account username will be your URL in the format yourusername.pythonanywhere.org, choose wisely.

Create an API token and access the pythonanywhere bash

Inside the shell, execute $ pip3.7 install --user pythonanywhere

A sample output is as follows:

Looking in links: /usr/share/pip-wheels
Requirement already satisfied: pythonanywhere in ./.local/lib/python3.7/site-packages (0.8.3)
Requirement already satisfied: python-dateutil in /usr/lib/python3.7/site-packages (from pythonanywhere) (2.8.0)
Requirement already satisfied: requests in /usr/lib/python3.7/site-packages (from pythonanywhere) (2.22.0)
Requirement already satisfied: docopt in /usr/lib/python3.7/site-packages (from pythonanywhere) (0.6.2)
Requirement already satisfied: six>=1.5 in /usr/lib/python3.7/site-packages (from python-dateutil->pythonanywhere) (1.12.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python3.7/site-packages (from requests->pythonanywhere) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3.7/site-packages (from requests->pythonanywhere) (2019.9.11)
Requirement already satisfied: idna<2.9,>=2.5 in /usr/lib/python3.7/site-packages (from requests->pythonanywhere) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/lib/python3.7/site-packages (from requests->pythonanywhere) (1.24.3)

We will now deploy our django app, use this command: $ pa_autoconfigure_django.py --python=3.7 https://gitlab.com/gian.mapacpac/communilytics.git

Sample output:

01:48 ~ $ pa_autoconfigure_django.py --python=3.7 https://gitlab.com/gian.mapacpac/communilytics.git

< Running API sanity checks >
   \
    ~<:>>>>>>>>>
Cloning into '/home/communilytics/communilytics.pythonanywhere.com'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 1310 (delta 20), reused 0 (delta 0), pack-reused 1268
Receiving objects: 100% (1310/1310), 15.96 MiB | 0 bytes/s, done.
Resolving deltas: 100% (898/898), done.
Checking connectivity... done.

< Creating virtualenv with Python3.7 >
   \
    ~<:>>>>>>>>>
Running virtualenv with interpreter /usr/bin/python3.7
Already using interpreter /usr/bin/python3.7
Using base prefix '/usr'
New python executable in /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/python3.7
Also creating executable in /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/preactivate
virtualenvwrapper.user_scripts creating /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/postactivate
virtualenvwrapper.user_scripts creating /home/communilytics/.virtualenvs/communilytics.pythonanywhere.com/bin/get_env_details

  _____________________________________________________________________
/                                                                       \
| Pip installing -r                                                     |
| /home/communilytics/communilytics.pythonanywhere.com/requirements.txt |
| (this may take a couple of minutes)                                   |
\                                                                       /
  ---------------------------------------------------------------------
   \
    ~<:>>>>>>>>>
Looking in links: /usr/share/pip-wheels
Collecting Django==2.2.3
  Downloading Django-2.2.3-py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 19.9 MB/s 
Collecting django-filters==0.2.1
  Downloading django-filters-0.2.1.zip (9.8 kB)
Collecting django-tables2==2.0.6
  Downloading django_tables2-2.0.6-py2.py3-none-any.whl (89 kB)
     |████████████████████████████████| 89 kB 1.7 MB/s 
Collecting django-adminlte3==0.1.0
  Downloading django_adminlte3-0.1.0-py3-none-any.whl (24.2 MB)
     |████████████████████████████████| 24.2 MB 18.5 MB/s 
Collecting django-crispy-forms==1.8.1
  Downloading django_crispy_forms-1.8.1-py2.py3-none-any.whl (108 kB)
     |████████████████████████████████| 108 kB 1.7 MB/s 
Collecting django-multi-captcha-admin==1.0.0
  Downloading django-multi-captcha-admin-1.0.0.tar.gz (4.3 kB)
Collecting django-multiselectfield==0.1.8
  Downloading django-multiselectfield-0.1.8.tar.gz (13 kB)
Collecting django-ranged-response==0.2.0
  Downloading django-ranged-response-0.2.0.tar.gz (3.0 kB)
Collecting django-simple-captcha==0.5.12
  Downloading django-simple-captcha-0.5.12.zip (245 kB)
     |████████████████████████████████| 245 kB 18.6 MB/s 
Processing /usr/share/pip-wheels/sqlparse-0.3.0-py2.py3-none-any.whl
Processing /usr/share/pip-wheels/pytz-2019.3-py2.py3-none-any.whl
Collecting six>=1.2.0
  Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Collecting Pillow!=5.1.0,>=2.2.2
  Downloading Pillow-7.0.0-cp37-cp37m-manylinux1_x86_64.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 22.7 MB/s 
Building wheels for collected packages: django-filters, django-multi-captcha-admin, django-multiselectfield, django-ranged-response, django-simple-captcha
  Building wheel for django-filters (setup.py) ... done
  Created wheel for django-filters: filename=django_filters-0.2.1-py3-none-any.whl size=7376 sha256=685f889f1c3ea83c765b50151b777e78ac6065c5742fe21ef17c480019322159
  Stored in directory: /home/communilytics/.cache/pip/wheels/79/a4/b8/e470d47a374f493ab8cb3b1f0d65826dcd8db0717291332656
  Building wheel for django-multi-captcha-admin (setup.py) ... done
  Created wheel for django-multi-captcha-admin: filename=django_multi_captcha_admin-1.0.0-py3-none-any.whl size=5095 sha256=16c49135213c08c3a9b8be47c35201e8ee4237c250a9fabb6528857ad95877ff
  Stored in directory: /home/communilytics/.cache/pip/wheels/a6/93/6f/862a216cb05e31b9cdd684888270ac55da36823ac1e947b45f
  Building wheel for django-multiselectfield (setup.py) ... done
  Created wheel for django-multiselectfield: filename=django_multiselectfield-0.1.8-py3-none-any.whl size=14291 sha256=714c199305910284b86bd3b316a0a130c40d0b3afdf8fc1c4a0289c4b03598de
  Stored in directory: /home/communilytics/.cache/pip/wheels/6d/4e/a3/ca75a79bad4d06c85f02e50d956b3867e6b2c25482f234ee37
  Building wheel for django-ranged-response (setup.py) ... done
  Created wheel for django-ranged-response: filename=django_ranged_response-0.2.0-py3-none-any.whl size=3132 sha256=bcb0f8a9b2d88c876c1ea7a263e4263b29660ac2ca7d191c3acca83825bb3ad0
  Stored in directory: /home/communilytics/.cache/pip/wheels/7f/b5/c8/6efaefd974a314385549443d93a00f281be8111842b4731be1
  Building wheel for django-simple-captcha (setup.py) ... done
  Created wheel for django-simple-captcha: filename=django_simple_captcha-0.5.12-py2.py3-none-any.whl size=90742 sha256=704aed45f8a457326c0a0ee13e7db06054dce8267bb694beeba7165ebbca83fc
  Stored in directory: /home/communilytics/.cache/pip/wheels/2e/bb/dd/e61777978f246b4c1cce3479e1361d766ec24cfb0936d61782
Successfully built django-filters django-multi-captcha-admin django-multiselectfield django-ranged-response django-simple-captcha
Installing collected packages: sqlparse, pytz, Django, django-filters, django-tables2, django-adminlte3, django-crispy-forms, django-multi-captcha-admin, django-multiselectfield, django-ranged-response, six, Pillow, django-simple-captcha


Successfully installed Django-2.2.3 Pillow-7.0.0 django-adminlte3-0.1.0 django-crispy-forms-1.8.1 django-filters-0.2.1 django-multi-captcha-admin-1.0.0 django-multiselectfield-0.1.8 django-ranged-response-0.2.0 django-simple-captcha-0.5.12 django-tables2-2.0.6 pytz-2019.3 six-1.14.0 sqlparse-0.3.0

< Creating web app via API >
   \
    ~<:>>>>>>>>>

< Adding static files mappings for /static/ and /media/ >
   \
    ~<:>>>>>>>>>

  _________________________________________________
/                                                   \
| Updating wsgi file at                             |
| /var/www/communilytics_pythonanywhere_com_wsgi.py |
\                                                   /
  -------------------------------------------------
   \
    ~<:>>>>>>>>>

< Updating settings.py >
   \
    ~<:>>>>>>>>>

< Running collectstatic >
   \
    ~<:>>>>>>>>>
Found another file with the destination path 'admin/js/collapse.min.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

2150 static files copied to '/home/communilytics/communilytics.pythonanywhere.com/static'.

< Running migrate database >
   \
    ~<:>>>>>>>>>
Operations to perform:
  Apply all migrations: CommunilyticsApp, admin, auth, captcha, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying CommunilyticsApp.0001_initial... OK
  Applying CommunilyticsApp.0002_auto_20200210_0944... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying captcha.0001_initial... OK
  Applying sessions.0001_initial... OK

< Reloading communilytics.pythonanywhere.com via API >
   \
    ~<:>>>>>>>>>

  ________________________________________
/                                          \
| All done!  Your site is now live at      |
| https://communilytics.pythonanywhere.com |
\                                          /
  ----------------------------------------
   \
    ~<:>>>>>>>>>


  ___________________________________________________________________
/                                                                     \
| Starting Bash shell with activated virtualenv in project directory. |
| Press Ctrl+D to exit.                                               |
\                                                                     /
  -------------------------------------------------------------------
   \
    ~<:>>>>>>>>>
(communilytics.pythonanywhere.com) 01:54 ~/communilytics.pythonanywhere.com (master)$ 
(communilytics.pythonanywhere.com) 01:54 ~/communilytics.pythonanywhere.com (master)$ 
(communilytics.pythonanywhere.com) 01:54 ~/communilytics.pythonanywhere.com (master)$ 

We now need to create an account to access our site:

(communilytics.pythonanywhere.com) 02:00 ~/communilytics.pythonanywhere.com (master)$ python3 manage.py createsuperuser
Username (leave blank to use 'communilytics'): admin
Email address: admin@localhost.com
Password: 
Password (again): 
Superuser created successfully.
(communilytics.pythonanywhere.com) 02:02 ~/communilytics.pythonanywhere.com (master)$ 

We are done! Visit yourusername.pythonanywhere.com to access your deployed site