TNS
VOXPOP
Do You Resent AI?
If you’re a developer, do you resent generative AI’s ability to write code?
Yes, because I spent a lot of time learning how to code.
0%
Yes, because I fear that employers will replace me and/or my peers with it.
0%
Yes, because too much investment is going to AI at the expense of other needs.
0%
No, because it makes too many programming mistakes.
0%
No, because it can’t replace what I do.
0%
No, because it is a tool that will help me be more productive.
0%
No, I am a highly evolved being and resent nothing.
0%
I don’t think much about AI.
0%
Python / Software Development

What Is Python’s Django?

This tutorial provides an introduction to Django, the Python web development framework.
May 14th, 2024 6:52am by
Featued image for: What Is Python’s Django?
Feature image via Unsplash.

It’s fair to say that no one learns how to code to fulfill their aspirations of building the best URL routing. But we now have seamless URL routing options. I imagine this was a byproduct of developers needing a uniform way to create routing as part of the larger web development process. Because really, who wants to deal with a routing issue when building out a fancy new feature? And it isn’t just routing. There are many unexciting parts of application building that still need to get done on almost every application – content management, authentication, serving static images. Luckily in the year 2024, we have web frameworks to abstract away the creation of many of these elements. In Python, there’s Django (and many more but let’s talk Django).

What Is Django?

Django is a popular and well-tested Python full-stack web framework. A full-stack framework typically provides the tools and libraries for both the frontend and backend development. Django can also be paired with other frameworks such as front-end frameworks React, Angular, and Vue. Django (like other frameworks) provides a set of guidelines and a baseline functionality for much of the overlapping functionality in applications. This includes database models, request routing, authentication, and HTML templating. Django, and other frameworks, abstract away much of the requirements freeing up developer time to focus more on what makes an application unique.

Why Django?

There are many tools that aid with web development. Why give Python’s Django the time of day?

Versatility

The Django framework is used in countless applications, some of them quite noteworthy. Instagram, Reddit, National Geographic, and Dropbox all use Django. This not only speaks to the diversity in what you can create within this framework since the business logic of these applications all differs but it also speaks to Django’s ability to scale. If you build your MVP in Django, you won’t necessarily need a full rewrite as your website scales as well.

Easyish to learn

It’s all relative but a lot of people learn and develop with Django. A lot of use means a lot of support. Django has a large community around it. There are countless tutorials, learning tools, and message threads to help you get started. Django also has support from the inside. Django is well-documented. Its comprehensive, easy-to-follow documentation makes it easy to find answers and solve problems. Django has a full ecosystem with well-maintained third-party modules with their own documentation. Not to mention, Django is open source so you don’t have to make a large investment to upskill.

Batteries Included

Django has an answer for most of the required functionality in modern applications, thus “batteries-included”. You never have to reinvent the wheel for basic tasks like URL routing, content management, authentication, and the serving of static files.

Django Basics

Django is modular. In place of a giant code file with all your functionality, or even on main app with different files, Django apps are broken up into smaller pieces appropriately called apps. Apps are mini projects within an application. There are no rules for how to create an app. Each app is a mix of unique functionality and common elements such as database models, URLs, and views (more on those later).

Let’s build a clothing shopping application in Django. We can break our site into a few different apps. Here’s a list of some apps this project could include:

Products app: This is the wide world of products including their management and presentation. For the products app to fulfill its duty, we’ll need to include the database models (more on database models later) for inventory, views for product listings, detail pages, search functionality, templates for rendering product pages, and URLs for routing product-related requests.

Users app: Here’s where we can store all our user data. This app will include a form for user sign-up and log-in, authentication, and profile management. The database models will focus around our user information. We can have views associated with each form and authentication with corresponding URLs for each.

Shopping cart app: Let’s build out the shopping cart app. The shopping cart functionality will need models for cart items and orders. The views will extend to adding/ removing products, viewing cart contents, and initiating the checkout process. This app will also include templates and URLs related to our shopping cart.

And this goes on and on until the shopping cart application is complete (if there is such a thing).

Django Fundamentals

Django has many fundamental elements. Here’s a little information a few of the fundamentals that were included earlier in this post.

Views: Views are the logic that handles processing a user request at any given URL or endpoint. When a user goes to a website built with the Django framework and makes a request (whether they realize it or not), the view associated with the URL is responsible for returning the response to that request. The response is typically an HTML template or JSON data. Django has function-based and class-based views. Function-based and class-based views differ in the way that they extend logic.

Sample view code

Models: Models define the database structure and provide a high-level abstraction for interacting with that data. By using models, we can create a class that represents a table. The attributes in this class represent each column inside the database table.

Sample model code

Object Relational Mapping (ORM): Django ORM automatically maps database tables to Python objects and vice versa. Developers can create, retrieve and delete records using Python methods create(), get(), and filter() handle those duties.

What Now?

Dive in. Check out this intro coding tutorial or visit the Django homepage for more tools on how to get started.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.