A step-by-step guide to deploy your django application on PythonAnywhere.
- Go to pythonanywhere.com
- Click on Console Tab
- Click on Bash Link
- Activate virtual environment with the following command: mkvirtualenv --python=python3.5 myproj
- use the command pip list to view all the packages that are already installed
- Use pip install django to install latest version of Django
- Get the application running in the instance by cloning from Github. Use git clone <HTTPS link> You will find the HTTPS link by clicking on Clone or download button in your github repository
- Use command ls to view all files and folders in the virtual environment.
- go into the project folder using cd <project_folder>
- Use command ls to view all files and folders inside the project folder
- Keep going inside until you find manage.py file
- Once you find manage.py, use the following command python manage.py migrate
- My application uses Pillow library so I get an error. I will install Pillow using the command pip install Pillow and then run python manage.py migrate again
- I have an app inside my project named mainApp, so I will use makemigrations command: python manage.py makemigrations mainApp
- Then run python manage.py migrate again
- Now we create superuser with the following command: python manage.py createsuperuser
- Fill in the username, password, email, etc. to create superuser
- If you get an error such as No module named argon2, you must install argon2 using following command: pip install django[argon2]
- Once argon2 is installed, create superuser using the command python manage.py createsuperuser
- Next go to pythonanywhere.com and click on Web tab
- Create a custom app by clicking on "Adda new webapp". Click Next. Click on "Manual configuration (including virtualenvs)". Click on Python 3.6. Hit Next
- It will first create a default application
- Scroll down to Virtualenv section and enter path to virtualenv
- Enter the follow: /home/<your user name>/.virtualenvs/<your virtual environment name>
- Click on "Start a console in this virtualenv" that appears now
- In the console type: ls
- Go inside folders using cd <folder name> until you find manage,py
- Type pwd and copy the path you get
- Go to anywherepython.com > web > under code section change source code to the path you have
- Click on the WSGI configuration file
- Delete lines between 13 and 47
- Go to # +++++++++++ DJANGO +++++++++++ and uncomment or add the following
- import os
- import sys
- path = '/home/vinitk/django-signup-login-and-admin/RefOne'
- Remember to change path = '/home/vinitk/mysite' to your path
- if path not in sys.path:
- sys.path.append(path)
- os.chdir(path)
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<path to settings>")
- In my case it is RefOne.settings
- import django
- django.setup()
- from django.core.wsgi import get_wsgi_application
- application = get_wsgi_application()
- Click on Save
- Go to Web tab > Click on the Configuration for link
- If you find an allow host error, then add the configuration for link to settings.py as follows
- ALLOWED_HOSTS = ['vinitk.pythonanywhere.com', ]
- Push it to github and pull from github in pythonanywhere console
- Under Web Tab click on Reload button and then click on configuration for link
- Now we setup static for static files to be accessible for the application
- Go to Web Tab
- Scroll to Static files and add the following url and directory
URL Directory /static/admin /home/<user>/.virtualenvs/<projectname>/lib/python3.6/site-packages/django/contrib/admin/static/admin /static/ /home/<projectfolder>/<projectfolder>/static URL Directory /static/admin /home/vinitk/.virtualenvs/myproj/lib/python3.6/site-packages/django/contrib/admin/static/admin /static/ /home/django-signup-login-and-admin/RefOne/static - To not let django display errors to users change DEBUG to False in settings.py file. To do this either change on your PC and git push to github and git pull to pythonanywhere server or go to Files Tab and go to settings.py and make changes in the editor provided by python anywhere.
- Click Save and under Web Tab reload the application
Your website is running and hosted on cloud server provided by PythonAnywhere.
Comments
Post a Comment