Configuring a staging deployment environment just to show a demo of your Django app is awful. Even more, if you only need a URL for a couple of minutes, to show it up to a client or teammate. The best option is to use a tool to expose your current local server. Exactly! People all around the world will be able to see what’s happening on your localhost. For that purpose, we will use Ngrok. This tool will allow us to create public URLs based on the local ports of our computers. All of this thanks to the magic of Tunnels. Before starting all the code worked on this tutorial, will be available in this Github repository.

Pre-requirements

Python 3.6+ installed in your system A little experience with Command-line interfaces Basic knowledge of Django Understanding of the Django-admin and manage.py scripts

It’s recommended to know all these basic things. Although it’s probable you can follow along with this tutorial, without further problems.

Creating a Django app

In order to create a step-by-step tutorial, I’ll explain the process of creating a Django project. If you already have one, you can skip this section.

Virtual environment

First of all, let’s create a Python virtual environment.

So open up your system terminal (or shell). If you find it too complicated to open your system terminal, try to use the built-in terminal of the code editor.

Click on Terminal>New terminal, and a shell should appear at the bottom of your screen. Type the following command to create a virtual environment, with the built-in Python tool.

It basically means:

Now if you list the files of your current directory, you should see a .venv folder.

To activate the virtual environment, we call the source command.

If you find any trouble, please refer to the following table extracted from the official python documentation.

Now if you have run the correct command, your shell should have the name of the virtual environment in it.

Install Django

After activating your virtual environment, now you are able to install whatever Django version you want. In this case, it’s a good choice to install the latest. Now it’s time to create the basic project structure of the Django app you want to expose. This will create a Django project with the name of mytestingproject. After creating it we want you will want to enter the directory where the project structure is located. So let’s cd into it and run the Django server. Now that you are in, let’s run the Django server to see how it looks. Basically, Django is telling us that a local server is running at our localhost (127.0.0.1 always points to it), on the default port, 8000. Don’t worry about the error message. First, let’s check all is working fine on our local server. To do that, go to your web browser and paste this direction: If everything is working fine, you should see a Django beautiful template.

That’s all about our Django installation. Now let’s get into exposing this Django project. If you are working on a more complex project, the following methods of exposing Django apps also will work.

Exposing a Django app with Ngrok

As I said before Ngrok is one of the tools that will let us land our local server on the internet. So first of all Download Ngrok from the official website. Once you have installed it, let’s proceed with the needed commands. Open another shell, keeping the terminal your Django server is currently running, and type. This will give you a quick look at the available commands that Ngrok offers, and also will grant that the installation process went correctly. In order to create a URL where people can access our server, let’s run. We are running the http argument, to create an URL that will connect to port 8000 of our localhost. Here is the result you’ll probably get. As it says, Ngrok is forwarding that weird and ugly URL, to our localhost. But the magic happens, when you hit the browser with the URL Ngrok gave you.

Changing settings.py

Wow, what just happened 😱? It seems Django is throwing an error because of a DisallowedHost setting. If you check the shell you are running the Django server in, and the one with the Ngrok session, you’ll get some debug messages. As Django is telling us, we must add the domain we are connecting to the ALLOWED_HOSTS config variable. But we have a problem, and it is that the domain name is too large and confusing. So let’s change a little bit the Django settings, to solve this error. Open the settings.py file located in the project folder. If you know some regex, you can appreciate that we’re setting a wildcard, where all the hosts will be allowed. Now reload the site, and see the result.

All is working perfectly now! And if you start to create applications for the project and setting URLs and views, all will be reflected on that public URL. Note: Don’t forget to change ALLOWED_HOSTS in production, since it would produce a huge security hole.

Conclusions

In this tutorial, you learned how to create a demo URL for your Django project, without the need of deploying it. You practiced how to start a Django project and work with the settings.py file in Django. Finally, you learned how to use Ngrok, and how to expose any local server with it. Next, explore some of the popular Python frameworks to build API.

How to Expose a Django Demo App to Internet with Ngrok  - 89How to Expose a Django Demo App to Internet with Ngrok  - 19How to Expose a Django Demo App to Internet with Ngrok  - 13How to Expose a Django Demo App to Internet with Ngrok  - 96How to Expose a Django Demo App to Internet with Ngrok  - 8How to Expose a Django Demo App to Internet with Ngrok  - 19How to Expose a Django Demo App to Internet with Ngrok  - 77How to Expose a Django Demo App to Internet with Ngrok  - 95How to Expose a Django Demo App to Internet with Ngrok  - 34How to Expose a Django Demo App to Internet with Ngrok  - 79