Categories
Online Marketing

Django Web App Development on the Raspberry Pi

So what is Django well Django is a web development framework that saves you time and makes web development a joy using Django. You can build and maintain high-quality web applications with minimal fuss, and we are all going to do that on the Raspberry Pi.

The Raspberry Pi is a tiny computer with enough power to run a Django development server. So here we’re going to attempt installing and deploying our first Django project on the Raspberry Pi and maybe develop a web application on later articles. You may be asking why don’t we just build a web application from scratch without a framework? Well, there will come a time when your application would be needing to connect to a database, and you would end up duplicating your database connection code to other parts of your application.

The practical way is to refactor it into a reusable function. Another problem you’ll encounter is you have to remember to close your database every time and you will end up with a lot of boilerplate code that is prone to mistakes. Your application may not be reusable in a different environment because of some environments, specific configuration. And lastly, if a web designer, who has no experience in coding, wants to redesign the page, he or she might crash the application, because, ideally the logic of the page or the retrieval of information from the database would be separate from the presentation which is the HTML Display of the page, these problems are precisely what a web framework intends to solve.

I’r sure some of you are already familiar with the MVC design pattern or Model View controller design pattern for beginners MVC, like what I mentioned earlier, is a django architecture that separates the logic, the model and the controller from the presentation. The view in django it’s more like an mtv or model template view design pattern. Don’t worry about that now, we’ll get more into that later.

In the meantime, let’s go ahead and install django on the raspberry pi. Okay, so I have here a freshly installed raspbian, which is the official operating system of the raspberry pi. What we’re going to do here is to open up a terminal and install virtual ends, which stands for a virtual environment. To put it simply, virtual environment is an isolated working copy of Python, which allows you to work on a specific project without worry of affecting other projects.

So let’s go ahead and install virtual end, but before we do that, let’s update our package lists by typing sudo. Apt-Get update and when it’s done updating we can go ahead and type in sudo, apt-get install virtual end hit enter, and in my computer, my or my raspberry pi, it’s already been installed. Now that we have our virtual environment installed. We can now create our virtual environment directory that we are going to use for our project to do that.

We just type virtual end dot. Then P, slash user, slash bin, slash Python 3. What this code does is. It makes a hidden directory called vent because in Linux beginning your directory name with the dot, makes that directory hidden and the dash B is an option. You pass virtual environment on what version of Python you want to use. In this case, we want to use Python 3 inside slash user USR, slash bin now hit enter, and when it’s done, if you type LS la you would see that it created our project directory as expected to use it.

We just type source that Ven slash bin. Slash activate you will see our prompt has changed, indicating that we are now in our isolated Python environment. We just type the activate to go back to our regular, prompt and escape our isolated Python environment. We can now install django. There are other ways to install django, but for simplicity, we’ll use pip to install the latest version of django available from PI P or the Python package index repository to do that.

We just type pip, install django and hit enter to test Jiang, the Django installation. You just start up a Python, interactive interpreter by typing Python, and if the installation was successful, we should be able to import Django and check its version. Okay. Moving on, we can now take our first step into creating our first project. A project is a collection of settings for an instance of Django, including database configuration Django, specific options and application specific settings.

So let’s exit our Python shell and type sy, Django admin, the PI or just Django admin, start project. Then the name of our project, which we’ll just name my site for now then hit enter okay. So it says that my site directory already exists just going to delete, delete it for now and make another one. So I’m just going to repeat the command Django admin that I start project my site and then hit enter.

What the start project command does is it creates a directory containing six files? Okay, there we are my site, which is the outer. My site directory is just a container for our project. It name doesn’t matter to Jango, you can rename it to anything. You like manage that PI py is a command line utility that lets you interact with this Jango project in various ways. Type Python manage that hi-oh. We just have to change into the directory first and type Python manage that py help to get a feel for what it can do.

Okay, so here are your options now you should never have to edit this file. It’s created in this directory purely for convenience. Okay, so let’s go back to files that were created. Oh now, it has added a few more files, but we’ll just go over on the basic files that we are concerned about. The next is the inner my site directory. It is the actual Python package for your project and we have our ini’ that py, it’s an empty file required for python to treat the my site directory as a package example a group of python modules – and we also have our settings that pi, which is the Settings configuration for this Django project take a look at it to get an idea of the types of settings available along with their default values.

Okay, so we’ll just skip that for now and we have our URL stat pot, the URLs that PI for this Django project is the table of contents of your jungle powered site. Next. Is we have our WSGI pi an entry point for WSGI compatible web servers to serve your project? And lastly, we have Ras GI that pipe as well as WSGI Jango also supports deploying on a SGI which is the emerging python standard for a synchronous, web servers and applications.

Now to see our barebone application in action. Let’s run the server by changing into the directory of our project, CD, my site and type the command that slash manage, that pi run server the run server launches the built-in django development server, which is a lightweight web server that we can use while developing our site. We will receive a warning message in red telling us we have unapplied migrations welljust.

We will just ignore that for now. Okay, now, if we open up our browser and go to HTTP colon, slash slash 127.0.0.1 colin 8000, which is the IP address that stands for home on port 8000 and if everything goes well, then we’ll see that it works by default. The run server command starts the development server on port 8000, listening only for local connections. If you want to change the server sport, pass it a command-line argument like so so, let’s just ctrl C and run our command again run server and we want to run it on port 8080.

So that’s how you change the port? Ok! So now we will access it here on port 8080. If you want other computers on your local network to be able to view your Django site by visiting your IP address in their web browsers, then you type manage that slash manage that pie run server. 0.0.0.0. 8000. Well, that’s all we have for now in this tutorial. If you have any questions or comments, please post it in the comment section below down below and I’ll do my best to answer them.

If you enjoy the article, please hit the like button and kindly support my blog by subscribing again. My name is Ryan and you are reading recurse TV see you next time.