Getting Started with Django: Build Your First Web Application

Django is a powerful and easy-to-use web framework written in Python. It helps you build secure, scalable, and maintainable web applications quickly by providing ready-made components.

If you’re new to Django, this guide will get you started with the basics — from installation to running your first project.


What Is Django?

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It comes with:

  • An ORM (Object-Relational Mapper) to work with databases easily

  • Built-in admin interface for managing site content

  • Secure authentication system

  • URL routing and templating engine

  • Tools for handling forms, sessions, and more


️ Step 1: Install Django

First, make sure you have Python installed (version 3.8+ recommended). Then, install Django using pip:

bash
pip install django

To verify the installation, run:

bash
django-admin --version

️ Step 2: Create a Django Project

Create a new project named myproject:

bash
django-admin startproject myproject
cd myproject

This sets up the basic directory structure and files.


Step 3: Run the Development Server

Start the server with:

bash
python manage.py runserver

Open your browser and go to http://127.0.0.1:8000/. You should see the default Django welcome page — congratulations, your first Django project is running!


Step 4: Create a Django App

Django projects are made of multiple apps — components handling specific functionality.

Create an app called blog:

bash
python manage.py startapp blog

Step 5: Connect Your App to the Project

Add the app to your project by editing myproject/settings.py and adding 'blog' to the INSTALLED_APPS list.

python
INSTALLED_APPS = [
# ... existing apps ...
'blog',
]

Step 6: Define a View

Create a simple view in blog/views.py:

python
from django.http import HttpResponse

def home(request):
return HttpResponse("Hello, Django!")


️ Step 7: Configure URL Routing

Create a new file blog/urls.py:

python
from django.urls import path
from . import views

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

Then, include it in your project’s main urls.py (myproject/urls.py):

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

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


Step 8: Test Your App

Restart your server (python manage.py runserver if it’s stopped), and go to http://127.0.0.1:8000/. You should see “Hello, Django!” displayed.


Summary

  • Install Django with pip install django.

  • Create a project using django-admin startproject.

  • Run the server and check the welcome page.

  • Create an app to organize features.

  • Write views to handle requests.

  • Set up URL routing to connect views to web addresses.

Leave a Reply

Your email address will not be published. Required fields are marked *