Introduction

Django is a powerful framework for building web applications, but deploying your app can seem daunting for beginners. PythonAnywhere offers a beginner-friendly and cost-effective solution to deploy Django apps for free. In this guide, we’ll walk you through the entire process of deploying your Django project on PythonAnywhere, from setup to going live.

Why Choose PythonAnywhere?

  • Free tier for small projects.
  • Built-in Python environment, no need for additional installations.
  • Easy-to-use web interface for managing files and databases.
  • Support for popular Python frameworks, including Django.

Prerequisites

Before deploying your Django app on PythonAnywhere, ensure you have the following:

  • A Django project ready for deployment.
  • A requirements.txt file listing your dependencies (use pip freeze > requirements.txt).
  • A PythonAnywhere account (sign up for free at pythonanywhere.com).

Step 1: Preparing Your Django Project

Before deploying, make sure your Django project is production-ready:

  • Update your ALLOWED_HOSTS in settings.py to include your PythonAnywhere domain (e.g., yourusername.pythonanywhere.com).
  • Set DEBUG = False in settings.py.
  • Run python manage.py collectstatic to gather static files.

Step 2: Uploading Your Project to PythonAnywhere

Follow these steps to upload your project:

  1. Log in to your PythonAnywhere account.
  2. Go to the Files tab and create a directory for your Django project.
  3. Upload your Django project files to this directory. You can do this via the web interface or by using scp for large projects.

OR, 

  1. You can go into a console and clone your git repo in there.

Step 3: Setting Up a Virtual Environment

PythonAnywhere requires a virtual environment for your project:

  1. In the Consoles tab, open a new Bash console.
  2. Create a virtual environment: python3 -m venv myenv.
  3. Activate the virtual environment: source myenv/bin/activate.
  4. Install your dependencies: pip install -r requirements.txt.

Step 4: Configuring the Web App

Now, configure your Django app to run on PythonAnywhere:

  1. Go to the Web tab and click Add a new web app.
  2. Choose Manual configuration and select the appropriate Python version.
  3. In the Code section, set the Source code to the directory containing your Django project.
  4. Specify your WSGI file path. Edit the WSGI file to include your Django app configuration:
    
    # +++++++++++ DJANGO +++++++++++
    # To use your own Django app use code like this:
    import os
    import sys
    
    # assuming your Django settings file is at '/home/myusername/mysite/mysite/settings.py'
    path = '/home/myusername/mysite'
    if path not in sys.path:
        sys.path.insert(0, path)
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
    
    ## Uncomment the lines below depending on your Django version
    ###### then, for Django >=1.5:
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    ###### or, for older Django <=1.4
    #import django.core.handlers.wsgi
    #application = django.core.handlers.wsgi.WSGIHandler()
        

Step 5: Setting Up the Database

If your app uses a database, follow these steps:

  1. Go to the Databases tab and create a new database.
  2. Update your settings.py to point to the new database.
  3. Run python manage.py migrate in the Bash console to apply migrations.

Step 6: Testing Your App

Visit your PythonAnywhere domain (e.g., yourusername.pythonanywhere.com) to test your app. If you encounter errors, check the following:

  • The Error logs in the Web tab.
  • Your WSGI file configuration.
  • Your settings.py for any incorrect configurations.

Step 7: Managing Static Files

To serve static files, configure the Static files section in the Web tab:

  1. Set the URL to /static/.
  2. Set the directory to the static folder in your project.
  3. Reload your web app.

Conclusion

Congratulations! Your Django app is now live on PythonAnywhere. This free deployment platform is an excellent starting point for small projects and testing your apps in a production-like environment. As your project grows, you can upgrade to a paid plan for more resources and features.

FAQs

1. Can I use a custom domain with PythonAnywhere?
Yes, PythonAnywhere supports custom domains on paid plans.
2. What is the free plan limit?
The free plan is suitable for small apps with limited traffic and resources.
3. Can I use a database other than SQLite?
Yes, PythonAnywhere supports MySQL and PostgreSQL for free and paid plans.

Start deploying your Django apps for free today with PythonAnywhere!