Use this guide to start a new Django project on your PythonAnywhere account.
Step 1: Set up a Django Development Environment on PythonAnywhere. You can follow this post for step by step instructions to setting it up.
Step 2: Go to the relevant directory on PythonAnywhere using the
cd command. Start a new Django Project by typing in the following command:
django-admin startproject mysite
Check the directory structure using
tree mysite command.
Step 3: Create a Web App on PythonAnywhere.
- Create a web app via PythonAnywhere's interface - this lets it know that you want to create a website at myusername.pythonanywhere.com and that it should listen and try to respond to any web traffic that comes to it for that domain.
- To create a web app on PythonAnywhere, open up a new browser tab and go to the Web tab. Click Add a new web app, choose Manual configuration and then Python 3.6.
- TIP: Make sure you choose "Manual configuration", not "Django" when creating your webapp. Manual option is needed to make sure it gets Django 2 from the virtualenv. When you hit next, you'll be on your web app configuration page, and it's probably a good idea to keep this tab open in your browser at all times too, so that you can easily jump back to it. It will allow you to easily hit reload on your web app, or find your error log, and things like that.
Step 4: Configure the web app to be run inside the virtualenv that you just set up
- This lets PythonAnywhere know which versions of python and django it should be using.
- Go to the virtualenv section of your web app and enter the name of your virtualenv. When you click confirm, you should find completes to a full path like this:/home/myusername/.virtualenvs/myvirtualenv
Step 5: Configure the web app so that it knows what actual code to run.
- Your wsgi.py file is how PythonAnywhere knows what code to run for you. Find the link to edit your wsgi file, delete everything and replace it with this:
import os import sys path = os.path.expanduser('~/mysite') if path not in sys.path: sys.path.insert(0, path) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.core.wsgi import get_wsgi_application from django.contrib.staticfiles.handlers import StaticFilesHandler application = StaticFilesHandler(get_wsgi_application())
- Working directory: Now go to the working directory section of your web app and add
/home/myusername/relevant_directory/mysite/as your project's working directory
- Allowed Hosts: Don't forget to add 'myusername.pythonanywhere.com' in allowed hosts in your new projects settings.py file.
Hit reload and visit 'myusername.pythonanywhere.com' in your browser. Your site should be live now.
Hope you found this post helpful.
In later posts, I will write about some more aspects of the Django web framework.