Getting to Know Django: Building web applications with Python

StarTek
5 min readDec 8, 2022

--

Are you ready to take the Python plunge and start building awesome web applications with Django? You’ve come to the right place! In this article, we’ll cover everything you need to know to get started with this powerful library.

Here are the key points we’ll cover:

  • What is Django and why should you care?
  • Installing Django and setting up your development environment
  • Your first Django project: a simple “Hello, World!” application

Now, let’s dive in!

What is Django and why should you care?

Django is a free and open-source web framework written in Python. It’s designed to help developers build secure and scalable web applications quickly and easily. With Django, you can take your ideas from concept to launch in no time.

But why should you choose Django over other web frameworks? Here are a few reasons:

  • It’s built on Python, one of the most popular and versatile programming languages in the world
  • It has a huge and supportive community, with plenty of online resources and tutorials to help you along the way
  • It’s well-designed and efficient, allowing you to build complex applications with minimal code
  • It’s secure and scalable, so you can be confident that your applications will perform well under heavy loads

In short, Django is an awesome tool for building web applications. And best of all, it’s a lot of fun to use!

Installing Django and setting up your development environment

Before we can start building our first Django project, we need to make sure we have everything set up properly. This involves installing Django and any other dependencies, as well as configuring our development environment.

First, let’s make sure we have Python3 installed on our system. Open a terminal and type the following command:

python3 --version

If you see a message like Python 3.x.x, you're good to go. If not, you'll need to install Python3 from the official website (https://www.python.org/).

Next, we need to install Django. This can be done with the following command:

pip3 install django

This will install the latest version of Django and any other dependencies it requires.

Once Django is installed, we need to set up our development environment. This involves creating a new Django project and configuring some basic settings.

To create a new project, navigate to the directory where you want to store your project files and run the following command:

django-admin startproject my_project

This will create a new directory called my_project, which contains all the files and settings for your project.

With that, our development environment is set up and ready to go!

Your first Django project: a simple "Hello, World!" application

Now that we have Django installed and our development environment configured, it's time to build our first project. As a classic example, we'll start with a simple "Hello, World!" application.

Let's create a new view for our app. A view is a Python function that takes a web request and returns a web response. In our case, the view will simply return a "Hello, World!" message. Run the following within your my_project directory (same location where manage.py is located).

python3 manage.py startapp hello_world

This will create a new directory called hello_world within your project directory. This directory contains all the files and settings for your app, including views, models, and templates.

Next, let’s open the my_project/settings.py file and make some changes. This file contains all the settings for your Django project, such as the database configuration, installed apps, and more.

First, we need to tell Django which database engine we want to use. By default, Django uses SQLite, which is fine for our purposes. If you want to use a different database engine, you can update the DATABASES setting accordingly.

Next, we need to add our first Django app to the INSTALLED_APPS setting. This tells Django which app(s) to load when the server starts. For our project, we only need to add the hello_world app, like so:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world',
]

Next, let's create a new view for our app. A view is a Python function that takes a web request and returns a web response. In our case, the view will simply return a "Hello, World!" message.

To create a new view, open the hello_world/views.py file and add the following code:

from django.http import HttpResponse

def hello_world(request):
return HttpResponse("Hello, World!")

This defines a new view called hello_world, which returns a HttpResponse object with the "Hello, World!" message.

Next, we need to map this view to a URL so that it can be accessed from a web browser. To do this, we need to create the hello_world/urls.py file with the following code:

from django.urls import path
from . import views

urlpatterns = [
path('', views.hello_world, name='hello_world'),
]

This defines a new URL pattern that maps the hello_world view to the root URL (/).

Next we need to include our hello_world page in our application. Add the following to my_project/ulrs.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello_world.urls')),
]

With that, our "Hello, World!" app is complete! To test it out, migrate and start the Django development server with the following commands:

python3 manage.py migrate
python3 manage.py runserver

This will start the server on your local machine, listening on port 8000. To access your "Hello, World!" app, open a web browser and navigate to http://localhost:8000/. You should see the "Hello, World!" message displayed on the page.

Conclusion

In this article, we learned how to get started with the Django Python library. We installed Django and set up our development environment, and then built a simple "Hello, World!" application.

Django is a powerful and versatile tool for building web applications. With its clean and efficient design, it's easy to see why it's one of the most popular web frameworks in the world. So why not give it a try and see what you can build? Happy coding!

I hope you’ve enjoyed reading this article and have learned something new. If you have, please consider subscribing to stay up-to-date with my latest content. Simply subscribing helps me out a lot and will allow me continue creating valuable information for you.

If you’re feeling extra generous, please consider making a donation using the link below. Your contribution will allow me continue to provide high-quality content.

Thank you for your support!

--

--