The Python application written using Django Framework!

The Python application written using Django Framework!

I've wanted to use Python and the Django Framework to develop a backend application for a long time. So here I am.

Concept of Django framework

A high-level Python web framework called Django makes it easier to create online applications quickly, cleanly, and scalable. By utilising the "batteries-included" concept, Django offers a comprehensive collection of tools and frameworks, freeing developers from having to start from scratch when building their application logic.

Benefits of Django

Rapid Development

Object-Relational Mapping (ORM) databases, template engines, URL routing, authentication, and other built-in Django capabilities make development easier and enable developers to create applications more quickly.

Scalability

Because Django is naturally scalable, developers can handle growing volumes of data and traffic without experiencing performance issues. Applications scale easily with its support for several deployment methods and flexible structure.

Security

Web applications must be secure, and Django provides built-in defence against prevalent security risks including SQL injection, clickjacking, cross-site request forgery (CSRF), and cross-site scripting (XSS). Django also promotes best practices for handling data validation and user authentication.

Versatility

Because of its versatility, Django may be used to create a vast array of applications, including social networks, APIs, e-commerce platforms, and content management systems. Because of its adaptability, developers can customise apps to satisfy particular business needs.

Community and Ecosystem

Django boasts a vibrant and supportive community of developers who contribute to its ecosystem by creating reusable components, plugins, and packages. This wealth of resources makes it easier for developers to extend Django's functionality and address complex requirements effectively.

Documentation and Support

Django provides thorough documentation that is an important tool for developers of all skill levels. Furthermore, the Django community ensures that developers have access to help whenever needed by means of forums, mailing lists, and online communities.

Let me tell you about the pre-requisites we need for this project:

VS Code (Visual Studio code as a IDE)

Python

Once you download these two and have it installed like any other software, all you need is the Django framework. Asking me where do you find that?

You can find all the instructions written in Django Framework Documentation.

But, since python is already on your machine, all you need is:

pip install django

Once you run the above command in the terminal of the VS Code, here is what you get to see:

Run "django-admin startproject <name of the application you would want to create>" to start creating the project or the application. Here I am creating an application called "ordernow". So my command will be:

django-admin startproject ordernow

When I run this, I can see the ordernow folder created on the left side navigation pane:

Just expand the pane to see it's components:

Now since my project folder was different from where the prompt was, I did a cd <foldername>, which in my case was:

cd ordernow

Once I was in the same directory, I had manage.py application code.

For now, understand this to be running as a simple server for you on the local machine. I wanted to understand how does this code display an output. So I ran the command below:

python manage.py runserver

Let's check what this code displays:

Boom!! A simple webpage welcoming us to use Django framework.

Once you run the runserver command on the VSCode and start to see this webpage, the VSCode starts generating logs. To see how it reacts if I use a different port, I used 5000 as the port instead of 8000, meaning to say instead of using

http://127.0.0.1:8000

I used

http://127.0.0.1/5000

and here was the log:

If you see the log in yellow colored font, it has this 404 at the end... meaning it did not reach the application at all. So changing the port back to 8000 got me a 200 status, which in web developer's language, is an OK status

Creating the application

Since I wanted to create an application for books, I chose the app name as books

Here is what I used to create the application:

django-admin startapp books

This command now creates a folder inside VSCode that has the name books and also provides the necessary files:

If I have to reach an application on the web, it needs routes. So I configured another file under the books folder and named it urls.py

Before we get there, we need to register/install our application. How do we do it? Navigate to the settings.py file under the ordernow folder, and scroll down to INSTALLED_APPS section. Just add the name of your application over here. In my case this was books, so I added books.

Include the paths of URL's now:

Since the server is running and it updates as you update the code, you can see the changes happening as and when you change the code.

I wanted to write a function/API that checks the health of my site and returns me a message. So I added this piece of code:

Once I did this, I could see the changes on the terminal

To check how the webpage was doing, I navigated to the URL once again, but with the route as /books/health :

localhost:8000/books/health

The message that I had coded was returned back to me! Baby step 1 accomplished!

Before we plan the webpage we would need some metadata for the application that we are creating. I had these on my mind, so I created a Class called Inventory and added them under the models.py file hosted under books folder. Note that for every file we write there are some modules that we import. This is a basic need in python and is readily available to us with VsCode's intellisense.

The next step was to start migrating this data into the server. Meaning, create a table into the database. So, I ran the command:

python manage.py makemigrations

While makemigrations was just a prepping step for the data entry into the table, there is another command that moves the data:

python manage.py migrate

The next step was to modify this webpage with the admin prompt and create a superuser for performing the CRUD operations. I ran the command :

python manage.py createsuperuser

These credentials used do not resemble those of any real user and are used just for the project. So I decided to go with the GOAT Virat Kohli to be my superuser.

To test if this works, I hit the below URL:

127.0.0.1:8000/admin

There we have it!! The admin dashboard for our web application. Now this is basic. We need to enhance some features on this.

So, under the admin.py of the books folder, edit the code:

Once the inventory is added through the code, check it on the webpage:

Great! That's success #2.

Let's add some inventory and check. I wanted to add one of my favorite books, so I added "Malgudi Days".

Strange isn't it? The object got added, but the details do not show up. That is when it struck me that we need to add some display-related features to this code. So I navigated to the admin file again and wrote some more modifications;

On checking this code modification now,

Booom!! There was a display with the content of the inventory. Great!! time to proceed one step ahead!

Let's create an API to get the list of books:

What did we do in the above step? We just created a function by name get_books and initialized a list called results. Now, results is a blank list. We now have to iterate through every item under books and get the data to append it to the list.

Make sure to add the route as well:

When you run the server once again, there is a high chance of a webpage showing you "In order to allow non-dict objects to be serialized set the sage parameter to false" error. I will show you a fix for this as I learn it.

I made the below modification to the code:

Once I did this and went back to my server URL, I could see this:

API Testing

For API testing, you can always go to POSTMAN. We can download the desktop version from this page.

Create a new collection on the home page once you install the Postman tool. Create a new request and choose GET as the method.

Once you see a success here, try to add an entry to the inventory. Select the POST method:

Click on "SEND"

You should be able to see this on the console:

To get all the weird formatting removed, there is something called as csrf exempt which we can use. To use the csrf exempt, we need to first import it from django.views.decorators.csrf module. In this module we have a tool called json.loads. This json.loads basically formats the body of the request.

Resend the request from the website and you can see this:

I want the API to do a true and false case . So I added the conditions:

Once we test the API from postman, we can now refresh the website:

Boom!! There we have the application doing everything we request!

Scope of the project

I have just demonstrated how we can add an entry to the inventory. Once you get rich in Python programming skills, you can implement the same for Remove, Update, and Delete operations as well.

Lastly, I would like to say that Coding is always about being creative and catering to customer needs. So happy learning!!