Python Tip 1: Accessing localhost Django webserver over the Internet

We use Django development webserver to test our application on localhost.

We can start the development webserver with below command:

Now one problem we face with this approach is that we can access/browse the project over the browser only in the local network to which our system is connected.

What if you want to show your project to someone at another location, in another city over the Internet?

How would you generate a public URL for your localhost webserver?

 

For such situations, there is one software available. ngrok.

How to use it:

– Download the ngrok from its download page .

ngrok download

–  Follow the documentation to unzip the downloaded content and to add the authentication token.

– Go to your project directory and activate your virtual environment.

–  Now start your Django development server at port lets say 8080.

– You can access your project on localhost:8080. We will generate a public URL for this localhost URL by creating a tunnel using ngrok.

– Go to the directory where you unzipped ngrok and run below command.

– You will see something like this:

ngrok tunnel

– Now you can access your project over the public address http://9f134cf4.ngrok.io  .

–  You can access the web interface on http://127.0.0.1:4040 to see which URL got how many hits.

Public URL for localhost Django project

 

You can host your Django App for free on PythonAnyWhere server.

Generating and Returning PDF as response in Django

We might need to generate a receipt or a report in PDF format in Django app. In this article, we will see how to generate a dynamic PDF from html content and return it as a response.

Create a Django project. If you are not using virtual environment, we strongly recommend to do so.

Installing Dependencies:

Once virtual environment is ready and activated, install the below dependencies.

For pdfkit  to work, we need wkhtmltopdf  installed in our Linux system.

Continue reading “Generating and Returning PDF as response in Django”

Get latest Bitcoin and other crypto-currencies rates using python Django

Everybody is investing in bitcoins. James Howells is trying to dig a landfill site to get 7500 bitcoins that were dumped there in 2013.

To be a good investor, it is necessary that you keep track of ups and downs in the market. There are multiple platforms where you can track the price of bitcoin. But for a python programmer that is no fun. Being a python programmer we will develop our own project where we can get latest bitcoin and other crypto-currency prices.

Let’s start.

Continue reading “Get latest Bitcoin and other crypto-currencies rates using python Django”

How to create management commands in Django

 

You must have used createsuperuser command in your django application at one or other time. If not then I am sure you must have used makemigrations or migrate commands in your project. Yes? Yes.

So these commands, also called as management commands are used to execute some piece of code from command line. In this article I will tell you how to create your own command.

Why do you need management command:

First thing that comes to your mind after reading above paragraph is that why do even I need management command in my project.

Lets say you have a project where you need to perform some task periodically. For example : updating database column after fetching data from some other server, sending lots of mails, taking backup of your database etc etc.

Continue reading “How to create management commands in Django”

How to start logging errors in Django app

In development or testing environment, you may set DEBUG=True  in your settings.py  file and whenever any error is thrown, you see it in the browser.

Setting DEBUG=True  in production environment is not recommended. Debug should be set to False on live servers so that stack trace is not visible in case of any error. Hackers or malicious users may exploit this and may attack you web application.

So how do you debug you application on live servers? Answer is Logging the errors in log files.

Lets  see how to start logging errors in django?

Continue reading “How to start logging errors in Django app”

Hello Word in Django : How to start with Django

In this article I will teach you how to make and run a python-django app in less than 5 minutes.

Prerequisite: Python (preferably python 3) is already installed.

Step to create and run the django app:
  • Install virtual environment. You may proceed without virtual environment too, but in long run it is going to be very helpful.
    $ pip install virtualenv

Continue reading “Hello Word in Django : How to start with Django”

How to host django app on pythonanywhere for free

So you developed a course project which you need to host somewhere so that everyone can access it.
Or you are working on some personal project and want to host it so that test users can test it.

What do you do in such scenario? You look for best hosting service provider. Right?
So what is best option to host your django app?

We tried multiple hosting providers and found out that best choice for a django app is pythonanywhere.com. why? Find out below.

Continue reading “How to host django app on pythonanywhere for free”