Uploading Django to pythonanywhere

Amaan
3 min readMay 18, 2019

--

In this tutorial I have tried cover up almost everything that you need to do to successfully deploy your django application to pythonanywhere.

Step 1: Upload project to github

You can directly upload the files to pythonanywhere but cloning your project from github is recommended.

Before uploading your file you also need to add one thing in the ALLOWED_HOST list inside settings.py file, that is your domain:

ALLOWED_HOSTS = ['username.pythonanywhere.com']

Otherwise, you will get INVALID HTTP_HOST_ERROR.

Step 2 : Clone the repository

Open up bash console in pythonanywhere and clone you code

git clone yourprojectlink

Step 3 Create and Activate virtualenv

virtualenv --python=python3.7 venvsource venv/bin/activate

Step 4 Install Django and other requirements neccessary

If your webapplication is only based on Django, then run:

pip install django

Or, if you have requirements.txt file, run:

pip install -r requirements.txt

Step 5 Settings Up DataBase (optional)

follow this step only if your application uses a database, Otherwise go for step 7.

Default database settings provided by Django: settings.py

You need to create a databse in pythonanywhere under the database section and set a password for it. After you have done that you your settings

#Production DB settings DATABASES = 
{ 'default':
{ 'ENGINE': 'django.db.backends.mysql',
'NAME': 'username$dbname',
'USER': 'username',
'PASSWORD': 'dbpassword',
'HOST': 'username.mysql.pythonanywhere-services.com',
}
}

Head back to the console and run migrations to populate the database setings, make sure you are in the directory containing the manage.py file.

$ python manage.py migrate

Under the Web section click on add new web app.

Click Next.

Then Select manual configuration then click Next.

Then choose the python version, and click Next.

After it finishes you will see a page containing a bunch of information. Right now our project is not linked to this webapp.

We will start by telling the webapp the path to the virtualenv. In the virtualenv input put the name of the virtualenv that we created before.

Step 8.1 Configuring wsgi file.

Open the wsgi configuration file

Scroll Down until you see, configuration related to Django.

# +++++++++++ DJANGO +++++++++++ 
# To use your own django app use code like this: import os importsys
# assuming your django settings file is at '/home/username/mysite'
# and your manage.py is is at '/home/amaanx/mysite/manage.py'
path = '/home/username/projectname' <------ a change
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings' <-b
# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

You can keep this part of the code and delete the other.

There you need to update two things:

a. The path to the project b. The name of the module that contains the settings.py file.

If you have wierd directory structure, i.e. you have the settings file deep inside the structure you would make changes to b accordingly

example:

somefile.otherfile.projectname.settings

Save changes and Reload the app. Open up the link and probably you webapplication will now be working.

If it doesn’t, you can check the errors at the error.log link. you can do a quick google search to solve the errors or you can post your error in the comments below, I will help you to solve them.

Security tip

Set DEBUG=False in the settings.py file, Once your application is live. As it can reveal the internals of your application and hackers can find way to break into your application.

Css files

It is likely that you might not be able to see your fancy css and static files like imags in your application after you deployed it. It is because Static files are served differently by different servers.

When you work on your site locally, Django itself handles the serving of static files.

But once you have uploaded your file to pythonanywhere you need to do a few things.

Configure settings.py

Add this line at the end,

in urls.py, add:

from django.conf import settings ... if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Then once you have done that push the changes.

And in pythonanywhere console, run the following command:

python manage.py collectstatic

This will collect all the static files and put it in a directory static_cdn.

Now you need to pythonanywhere about this directory, head over to the web section. Scroll down until you see Static Files section.

In URL put : /static/ In path put: /static_cdn/

Now reload the Application, If you can’t see the changes try to clear the cache of your browser and reload again.

Originally published at https://pengoox.pythonanywhere.com.

--

--