How to Build your own website using Django in Python

Django is a Python web framework that is both free and open source. It follows the Model-View-Template (MVT) architecture pattern and enables rapid development of secure, scalable web applications with minimal code.

Why Use Django?

  • It's very fast and optimized for performance.

  • Comes with built-in features like user authentication, admin interface, site maps, and RSS feeds.

  • It is very secure and prevents common vulnerabilities like SQL injection, cross-site scripting, and clickjacking.

  • It is highly scalable and can handle high network traffic efficiently.

Now that you understand the advantages of Django, let's start building your first web application.

Setting up a Virtual Environment

A virtual environment creates an isolated Python workspace for your Django project, preventing conflicts with other projects.

First, install the virtualenv package:

# Install virtualenv
# Run this in your terminal/command prompt
python -m pip install virtualenv

Create a project folder and set up the virtual environment:

# Create project directory
mkdir django-intro
cd django-intro

# Create virtual environment
virtualenv env

# Activate virtual environment
# On Windows:
env\Scripts\activate
# On macOS/Linux:
source env/bin/activate

You'll see (env) in your terminal prompt when the virtual environment is active.

Installing Django

Ensure you have Python 3.6 or above installed, then install Django:

# Install Django
import subprocess
import sys

# This simulates: python -m pip install Django
print("Installing Django...")
print("Command: python -m pip install Django")

Verify your Django installation:

python -m django version

Creating a Django Project

Create the project skeleton using Django's admin utility:

django-admin startproject mywebsite
cd mywebsite

This creates a project structure with the following files:

  • manage.py − Command-line utility for project management

  • settings.py − Project configuration and settings

  • urls.py − URL routing configuration

  • wsgi.py − Web Server Gateway Interface entry point

Creating Your First App

Django projects consist of multiple apps. Create your first app:

python manage.py startapp blog

This creates an app directory with these files:

  • views.py − Contains view functions that handle requests

  • models.py − Defines database models

  • admin.py − Admin interface configuration

  • apps.py − App configuration

Registering the App

Add your app to the project's INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',  # Your new app
]

Running the Development Server

Start the Django development server to test your application:

python manage.py runserver

Open your browser and navigate to http://127.0.0.1:8000/ to see your Django welcome page.

Creating a Simple View

Create a basic view in your app's views.py file:

from django.http import HttpResponse

def home(request):
    return HttpResponse("<h1>Welcome to My Django Website!</h1>")

def about(request):
    return HttpResponse("<h1>About Page</h1><p>This is my first Django app.</p>")

Project Structure Overview

File/Directory Purpose
manage.py Command-line utility for managing the project
settings.py Project configuration and database settings
urls.py URL patterns and routing
views.py Functions that handle HTTP requests and responses
models.py Database models and schema definitions

Next Steps

To build a complete website, you can:

  • Create HTML templates for dynamic content rendering

  • Set up URL patterns to connect views with specific routes

  • Design database models for data storage

  • Add CSS and JavaScript for styling and interactivity

  • Implement user authentication and authorization

Conclusion

You have successfully set up a Django development environment, created your first project and app, and understand the basic project structure. Django provides a solid foundation for building robust web applications with Python's simplicity and power.

---
Updated on: 2026-03-25T15:58:44+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements