Getting Started with Django: Build Your First Web App with Ease!

Muhammad Abdullah Arif
4 min readFeb 15, 2024

--

Are you eager to dive into web development but unsure where to start?

Photo by Slidebean on Unsplash

Look no further! Django, a high-level Python web framework, is here to simplify your journey into the world of web development. In this blog post, we’ll guide you through the basics of Django, empowering you to create your very own web application with joy and ease.

Photo by Faisal on Unsplash

What is Django?

Django is a powerful web framework that enables developers to build web applications quickly and efficiently. Developed using Python, Django follows the principle of “Don’t Repeat Yourself” (DRY), allowing developers to write clean, maintainable code without redundancy. Whether you’re a beginner or an experienced developer, Django provides a robust set of tools and features to streamline the development process.

Setting Up Your Django Environment

Before we begin building our web app, let’s ensure we have Django installed on our system.

You can easily install Django using pip, Python’s package manager. Simply open your terminal or command prompt and enter the following command:

pip install django

Once Django is installed, we can create our Django project. Navigate to your desired directory and run the following command:

django-admin startproject myproject

Replace “myproject” with the name of your project. This command will create a new directory containing the necessary files for your Django project.

Creating Your First Django App

Now that our project is set up, let’s create our first Django app. Within your project directory, run the following command:

python manage.py startapp myapp

Replace “myapp” with the name of your app. Django will create a new directory for your app containing various files and directories.

Defining Your Models

Models in Django represent the structure of your data. Open the “models.py” file within your app directory and define your models using Python classes. For example:

from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __str__(self):
return self.name

Creating Views

Views in Django handle the logic of your web application. Open the “views.py” file within your app directory and define your views. Views are Python functions that receive HTTP requests and return HTTP responses. For example:

from django.shortcuts import render
from django.http import HttpResponse
def my_view(request):
return HttpResponse("Hello, Django!")

Mapping URLs

URLs in Django define the mapping between URLs and views. Open the “urls.py” file within your app directory and define your URL patterns. For example:

from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.my_view, name='hello'),
]

Running Your Development Server

Now that our project and app are set up, let’s run the Django development server. Navigate to your project directory and run the following command:

python manage.py runserver

Open your web browser and navigate to http://localhost:8000/hello/. You should see “Hello, Django!” displayed in your browser, indicating that your Django app is up and running.

Conclusion

Congratulations! You’ve successfully created your first Django web app. With Django’s simplicity and power, you’re now equipped to explore further and build even more advanced web applications.

Whether you’re building a personal portfolio, a blog, or a full-fledged web application, Django has everything you need to bring your ideas to life. Happy coding!

Click on the link below to discover a wealth of knowledge and explore a variety of engaging topics.

Medium Profile: Muhammad Abdullah Arif — Medium

Stay Up-to-Date with Muhammad Abdullah Arif’s Latest Publications — Subscribe Now! (medium.com)

I wish you all the best in your future endeavours.

--

--

Muhammad Abdullah Arif

Python developer. The facts are the facts but opinions are my own.