How to Deploy Django Project on PythonAnywhere?

Topics Covered

Overview

Django is one of the popular frameworks of Python. We can create projects in Django. After creating the project if we want to make that project available for all then we deploy the project on PythonAnywhere. PyhtonAnywhere is a Python web hosting service. In this article, we will look at how to deploy the Django project on PythonAnywhere.

Setup Your Django Project

Firstly let us create a Django project with the name exampleProject by writing the below command in the terminal.

Now open the settings.py file of the project and update the ALLOWED_HOSTS = ['*'].

We will create a file requirements.txt by writing the command given below:

Now run your project by writing the below command.

After running this command our project will run and we will get the following output on the browser.

setup your django project

Upload Project to GitHub

Now we need to upload the project on GitHub, To upload a project follow the YouTube video of uploading the project to GitHub

Deploy Project on PythonAnywhere

  1. The next step is to create an account on pythonanywhere by entering the details.

    deploy project on pythonanywhere

  2. After registration a page like the below page will open in front of you.

    deploy project on pythonanywhere

  3. Now click on the bash option available inside the console box. Then the below page will appear on your screen.

    deploy project on pythonanywhere

  4. Now copy the link of GitHub repository link of your project.

  5. And type the below command in bash.

  6. Now type the below command to create the virtual environment.

  7. Activating the virtual environment by writing the below command.

  8. Type the command given below for installing requirements.

  9. Now go to the web available in the side menu.

    deploy project on pythonanywhere

  10. Click on Add a new web app.

    deploy project on pythonanywhere

  11. Now click on the next.

    deploy project on pythonanywhere

  12. Click on the Manual Configuration option.

    deploy project on pythonanywhere

  13. Select Python 3.10 version.

    deploy project on pythonanywhere

  14. Under the code section. Enter the path of our project code.

    deploy project on pythonanywhere

  15. Goto VirtualEnv section.

    deploy project on pythonanywhere

  16. Click on Enter the path of Virtualenv and enter the course of our virtual environment.

    deploy project on pythonanywhere

  17. Under the Code section open the WSGI configuration file.

    deploy project on pythonanywhere

  18. Edit the file at lines 12 and 17, in place of the word mysite write the name of your project which has been cloned from Git Hub.

    deploy project on pythonanywhere

  19. Our project name is exampleProject, so we will write exampleProject in place of mysite.

    deploy project on pythonanywhere

  20. Save the file.

  21. Now reload it.

  22. Now open the link of your project it will work.

Problem Faced while Hosting Python Website on PythonAnywhere

  • All the required packages of Python will be installed before creating the web app so that there will be no error at the time of hosting.
  • For the integration of JS, and CSS files in a Django provide the path of static files in the STATIC FILE SECTION.

Conclusion

  • After creating the Django project if we want to make that project available for all then we deploy the project on PythonAnywhere.
  • We have to create account create an account on pythonAnywhere to deploy our project.
  • All the required packages of Python will be installed before creating the web app so that there will be no error at the time of hosting.