COMPUTER SCIENCE
Journey into Django



SHARATH PAI
11 FEB 2024 · 6 MIN READ

image

I previously wanted to get into MERN Stack Development, but since transitioning into the field of AI, I have started working with Django frequently and found the learning curve quite interesting. Django is a backend development framework written in Python. Here are the key things that I learned and implemented in Django.

1. Basics

While working with ReactJS, we needed to create only the project directory. But in Django, we need to create the project as well as the app directories. The project directory contains all the main setups that you require for your apps. The advantage of using Django is that you can create multiple apps in one single project.

2. MVT Architecture

Many of us might have heard of MVC architecture while learning NodeJS. MVT architecture works quite similarly. MVT stands for Model View Template. A model is very similar to creating tables for a form in a database. It controls the validation of the fields inside a table. Views are the heart of a Django project. It handles all the requests as well as the logic for each table. Templates are the front-end part where you use HTML, CSS, and JS to design your web application.

3. URL and Slug Creation

The most important part of a backend website is the URL. The creation of URLs is relatively easy, as you need to define your URLs in the urls.py file and write the redirection logic inside the views. Additionally, we can pass sub-routes called slugs by passing keyword arguments into the requests function. This will create a sub-route of a route.

4. Template Inheritance and Context

While building a website using traditional HTML, CSS, and JS, for each page we had to write the boilerplate template of HTML again and again. Using the template inheritance concept, we just need to write the boilerplate only once, which acts as a parent template. Then we can use Django blocks, and while creating each page, we only need to write the contents of our tag. Interesting, isn't it?

If we want to create a page, say a page for team members,  the HTML will be the same and just the information will change. If we are using traditional HTML and CSS, we have to write the HTML again and again for each team member. In this case, we can use context in Django. Context is defined in dictionaries, and each team member will have a distinct dictionary but the same headings. Then we can map the content into HTML using Django blocks. In this way, we only need to write the HTML once, and if any team member gets added, we can add another dictionary containing their information in the Views directory. In this way, context can be used for those areas in which the HTML is the same for each piece of information.

5. Database Administration and Authentication

One of the major advantages of using Django is that it comes with a built-in SQLite database for creating projects. As said earlier, for each form, we can configure it in the Models and Views directories, and when a user fills out the form, the data will be stored in the database. This database is also called Django Admin. In Django admin, we can assign users and see the information of forms filled by the users. Additionally, we can use authentication, where we need the users to login to the website and write all the necessary logic. Authentication is very simple in Django as compared to JavaScript.

6. Object Relational Mapping

If we need to access any particular information in the database, we need to visit each response to check the required information. This can be time-consuming if there are a large number of people who have filled out the form. In this case, ORM comes into the picture. We can filter out or find the necessary information using the shell with just a single command instead of extensively searching it into the database.

7. Integration with React

Since getting into Django, I have never really needed to use any kind of JavaScript snippet. But as a former React developer who used React as his main tech stack in every single project, I was curious to know if Django could be integrated into React. And yes, there were tutorials on how to integrate both of them. Inside the template directory, we can use our React App, which makes it easier to create components, something Django doesn't have. I was never really required to create components in Django, but in the future, I'll be looking forward to working with both of them together.

8. Product Development out of ML Models

Perhaps the main reason why I switched to Django from the MERN stack is that Django provides integration machine learning models into a web application. Working with AI and having worked with web development before, I wanted to create a fusion of both for my upcoming projects. Even in one of my internships, there was an optional task to create a prototype of our AI models for small and medium businesses. Since then, I have decided to start learning and working with Django. The ML model that provides the best result can be stored in the Pickle module of Python (there are several other libraries too for storing ML models), and it can be imported into our Django application, and out of that, we can create a web application out of the model.

9. Deploying the Project into Production

After you create a Django project, there are certain steps to be followed while deployment. I use Vercel which is an open-source cloud platform for deployment of web applications. In my initial project, I found this stage tough. But slowly and steadily, I debugged all the errors and created a documentation which included the steps to be followed and things to be taken care of. I'll explain in short. We need to create vercel.json file, add a code snippet based on runtime logs and add a build file for installing all the dependencies. The dependencies will be stored in requirements.txt. Additionally, I noticed that Vercel doesn't support SQLite DB so you need to configure your database using Postgres. We also need to add the host which we are using locally and add static urls to the urls.py file. We also need to make sure about the Python and Django versions we are using. For a safer side, I would suggest you'll to use Python 3.10.9 and Django 4.2.8. Don't upgrade to the newer versions at any time. This was the point where I was getting errors. It took me a month to figure out the errors and understand the fundamentals of production engineering for Django. Finally all the project files are ready to be deployed using GitHub and Vercel.



In short, switching to Django has been one of the best decisions since I never really understood the workings of NodeJS apart from URLs. The JS snippets are very complicated, and there are high chances of me getting errors in every single line of code. I found backend development really tough compared to many people. At that point, I decided to continue working with AI since I could do nothing significant in web apart from frontend. After building a strong foundation in Python through AI, I decided to continue with backend development again, but this time with Django. After learning it, I understood the concepts really well, and now even I find backend development very easy.

Overall, I would say that you do not need to learn every language or framework. Choose your field wisely, and then decide your tech stacks. In my case, learning MERN would mean learning extra technologies, so I chose Django since it is built in Python and I can work on both web development and AI using a single framework. Like me, if you're choosing AI or ML as your main career but want a side of web development, I would highly recommend you use Django or Flask as your backend, and you need not waste time learning MERN Stack.

That's all I learned about Django. Although there are some more things to learn, all the above points helped me develop a strong base in Django and backend development, and I can at least build the applications required for my projects.