Skip to main content

Hosting your Django app in Cloud Based Server


Let’s say that you have a solid business plan and you decide to go with Python and Django framework to take this project from concept to reality. One of the most fundamental things to do at this stage is to find a Django-friendly hosting service.

Since Django framework has a special syntax and structure, it’s important to make sure that a chosen provider makes it possible to easily deploy and scale any Django project.

There are many Django-compatible hosting providers which are worth considering. This post documents my experience deploying a basic Python based web app in " PythonAnywhere". PythonAnywhere runs on powerful Amazon EC2 servers and it is pre-configured with tons of libraries.

Steps:

  • Create a free user account in GitHub.com (If you haven't done it yet!)

          Then, create a new repository, giving it the name. My test project was named "DjangoTest".

  • Set up our Django Test App on PythonAnywhere

    • Sign up for a PythonAnywhere account in www.pythonanywhere.com                         PythonAnywhere is a service for hosting Python code on servers "in the cloud". Sign up for a "Beginner" account on PythonAnywhere (You don't need a credit card).   
    • Note:  When choosing your username here, remember that your blog's URL will be in      the form "yourusername.pythonanywhere.com". So choose an appropriate name (e.g.        for what your web app is all about).
  • Create a PythonAnywhere API token

          This is something you need to do only once. Sign in to your "PythonAnywhere" account and 
          go to your dashboard. Find the link near the top right to your "Account" page and then select 
          the tab named "API token". Click on "Create new API token" button.

  • Configure web app on PythonAnywhere

    • Go back to the main PythonAnywhere Dashboard and choose the option to start a "Bash" console(which is a PythonAnywhere version of a command line)
    • Deploying a web app on PythonAnywhere involves pulling down your code from GitHub, and then configuring PythonAnywhere to recognize it and start serving it as a web application. There are manual ways of doing it, but PythonAnywhere provides a helper tool that will do it all for you. Let's install it first:
                    Note: Remember to run the below command in PythonAnywhere command-line and not 
                    in your PC, otherwise cloning with fail.

                    $ pip3.6 install --user pythonanywhere

                    That should print out some things like Collecting pythonanywhere, and eventually end 
                    with a line saying Successfully installed (...) pythonanywhere- (...).
    • Now we can run the helper to automatically configure our app from GitHub. 
                    Type the following into the console on PythonAnywhere (use your GitHub username in 
                    place of <github-username>):
                              
                    $ pa_autoconfigure_django.py https://github.com/<github-username>/DjangoTest.git

                    As you watch that running, you'll be able to see what it's doing:
      • Downloading your code from GitHub
      • Creating a virtualenv on PythonAnywhere, just like the one on your own computer
      • Updating your settings file with some deployment settings
      • Setting up a database on PythonAnywhere using the manage.py migrate command
      • Setting up your static files (we'll learn about these later)
      • And configuring PythonAnywhere to serve your web app via its API
                    On PythonAnywhere all those steps are automated, but they're the same steps you would 
                    have to go through with any other server provider.
  
                      
  •  Initialize the admin account with a super user  

           PythonAnywhere has automatically activated your virtualenv for you, so all you need to do is 
           run the below command in PythonAnywhere command-line

           (user_name.pythonanywhere.com) $ python manage.py createsuperuser

           Type in the details (username, email and password )for your admin user.Now, you can also 
           take a look at your code on PythonAnywhere using ls command in PythonAnywhere 
           command-line:

           (user_name.pythonanywhere.com) $ ls

  • You are now live!

           Your site should now be live on the public Internet! Click through to the PythonAnywhere 
          "Web" page to get a link to it.


Comments

Popular posts from this blog

Solving PyCharm bug: "Python helpers are not copied yet..."

I have been using PyCharm Professional to run and debug Python code from my Windows machine to a remote Linux device. There are some other tools available for the same purpose (e.g. Python Tool for Visual Studio 2017). But in my opinion, PyCharm Professional stands out among its counterparts as it comes with a "All Batteries Included" setup. Once you configure remote Python interpreter in PyCharm, then it works out of the box. However, today after upgrading to PyCharm Professional 2018.2.1, I could not run my Python script on remote device. The execution always failed with below error - "Error running 'hello': Python helpers are not copied yet to the remote host. Please wait until remote interpreter initialization finishes." To solve this issue, I had to remove the ".pycharm_helpers " folder from the remove device and then restart PyCharm so that the folder is re-created and files are copied again. Here are the steps with comma

Customized Crosshair with OxyPlot charting library

I am using Oxyplot to draw heatmap for a WPF project and needed to draw a crosshair that moves according to user clicks. Since I couldn't find any good example or documentation on the topic, I made a small hack by using LineAnnotations and by over-riding Oxyplot mouse click event. Here is the sample code using WPF with MVVM pattern- MainWindow.xaml <Grid>         <oxy:Plot x:Name="CrossHairPlot">             <oxy:Plot.Axes>                 <oxy:LinearAxis Key="MyXAxis" Position="Bottom" IsZoomEnabled="False"/>                 <oxy:LinearAxis Key="MyYAxis" Position="Left" IsZoomEnabled="False"/>                 <oxy:LinearColorAxis Key="ZAxis" Position="Top" LowColor="Black"                                                                                      HighColor="White" PaletteSize="300">                     <G

Solving ‘Could not resolve host: github.com’ issue

Recently I ran into this issue while trying to clone one of my own repository from GitHub C:\GitHub\FS> git clone https://github.com/<username>/FS.git Cloning into 'FS'... fatal: unable to access 'https://github.com/<username>/FS.git/': Could not resolve host: github.com At first I thought that this could be due to some proxy setting. So I tried to unset it by executing below command - C:\GitHub\FS> git config --global --unset https.proxy But that didn’t fix the problem and I was still getting the same error. As it turned out later, the culprit was my VPN connection. Turning off the VPN fixed the issue. For the records, I was using ‘Surfshark’ on my Windows 10 laptop.