deploy django app to python

How to deploy Django apps to Python Anywhere through GitHub?

Let’s look at what is GitHub?

GitHub is a web-based interface that uses Git, the open source version control software that lets multiple people make separate changes to web pages at the same time. As Carpenter notes, because it allows for real-time collaboration, GitHub encourages teams to work together to build and edit their site content. In this blog we will know how can we deploy Django apps to python.

                   

Why use GitHub?

There are a number of reasons.. The first is that it enables slick and easycollaboration and version control. This allows you to work on code with anyone from anywhere. Additionally, many employers use GitHub. So, if you plan on getting a job, you’ll look really good if you already know your way around GitHub. And don’t forget about the connections, learning, and portfolio aspects. GitHub is a robust learning and collaboration platform. Take time to explore it and see just how much it can expand your programming knowledge.

Common terms for GitHub

  • Repository (repo) β€” a folder in which all files and their version histories are stored.
  • Branch β€” a workspace in which you can make changes that won’t affect the live site.
  • Markdown (.md) β€” a way to write in Github that converts plain text to GitHub code.
    Sites such as Atom and Sublime Text are examples of free resources for developers
    using Markdown.
  • Commit Changes β€” a saved record of a change made to a file within the repo.
  • Pull Request (PR) β€” the way to ask for changes made to a branch to be merged into
    another branch that also allows for multiple users to see, discuss and review work
    being done.
  • Merge β€” after a pull request is approved, the commit will be pulled in (or merged) from one branch to another and then, deployed on the live site
  • Issues β€” how work is tracked when using git. Issues allow users to report new tasks and content fixes, as well as allows users to track progress on a project board from beginning to end of a specific project.
  • Federalist β€” a platform that securely deploys a website from a GitHub repository in minutes and lets users preview proposed and published changes.

How do I Use a GitHub?

1. Sign up for GitHub
2. Install Git
3. Create a Repository
4. Create a Branch
5. Create and Commit Changes to a Branch
6. Open a Pull Request
7. Merge Your Pull Request

So, I thought it would be amazing if we can just push code onto GitHub and GitHub automatically updates the code on PythonAnywhere and not to mention the default features that are provided by GitHub like Pull Requests, Issues, Actions(haven’t used them till now still a feature) and everything else that GitHub offers.

How?

We will be using something known as Deploy Keys and Webhooks provided by GitHub to
tell our Django Web-App tha it has been updated so it pulls the latest code.
Step 1: Install the Python package named GitPython by using the following command:

Step 2: Write the view function that will receive the update and update the code on the server.
Go to your views.py and add the following code:

You can also send response codes with them by passing the parameter “status” within the
“HttpResponse” that will be returned.

Step 3: Set the URL where the Payload from GitHub will be sent.

To add the payload URL add the following lines into urls.py file of your Django Project:

Step 4: Now push this code to GitHub and login to our account on PythonAnywhere.

Now follow the following steps:
1: Open a bash terminal on PythonAnywhere.
2: Issue the following command:

after this command you will be shown the following prompt:

3: When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

4: At the prompt, type a secure passphrase.

5: Issue the following command on the bash terminal of PythonAnywhere to get your key.

Step 5: Go to your GitHub Account and then Go to the Repository in which you have your Django Project.
Then follow the following steps:
1: From your repository, click Settings as shown below.
2: In the sidebar, click Deploy Keys, then click Add deploy key.

3: Provide a title, paste in your public key.

4: Select Allow write access if you want this key to have write access to the repository. A deploy key with write access lets a deployment push to the repository.
5: Click Add key.

Step 6: Go to your PythonAnywhere account and go to the bash terminal and setup your Web-App but use the SSH instead of HTTPS for cloning your Repository from GitHub.

Note: I would suggest to use the automated script pa_autoconfigure_django.py available in pythonanywhere package which can be installed via pip to automatically setup my project on PythonAnywhere.

If you already have a project setup on PythonAnywhere using HTTPS then open a bash terminal and edit the remote URL to set SSH by using the following command :

Step 7: Go back to the settings tab of your Repository on GitHub and follow the following steps :
1: In the sidebar, click Webhooks, then click Add webhook.

2: Fill in the Payload URL and set the “Content type” to “application/json” as shown below.

3: click “Add webhook“.

Atlast: You will see the following when it is success

If it show some other icon in place of a green tick then click on edit and at the bottom you can see the “Recent Deliveries” here you can click on the button shown below to see the details of that delivery.

After clicking you will see two tabs “Request” and “Response” to see the details of failure you can click on response and you will get the details as shown below

If you get any response code other than 200 then go to the error log on your PythonAnywhere account and check what’s the problem. Usually people make mistake when they return something else instead of HttpResponse then it will throw someAttribute error.

We’re delighted you made it to the end of the blog, and I hope you’ve learned something new. Check out our other python blogs as well.

If you wish to write a blog for us or make any suggestions to the post that you believe would be valuable to other users, please send an email.

Every suggestion is good, and we are appreciative for your visit.