Building Your First Website with Django: A Step-by-Step Guide

Md. Tanvir Reza
2 min readJan 4, 2024

--

Introduction:

Are you ready to dive into web development and create your website? Look no further! In this tutorial, we’ll walk through the process of building a website using Django, a high-level Python web framework. Django makes web development fast, secure, and scalable, allowing you to focus on building your application rather than dealing with the nitty-gritty details of web development.

Prerequisites:

Before we begin, make sure you have the following installed on your machine:

  1. Python: Django is a Python web framework, so ensure you have Python installed. You can download it from python.org.
  2. Django: Install Django using pip, the Python package installer, by running the following command in your terminal or command prompt:
pip install django

Step 1: Create a Django Project:

Open your terminal or command prompt and navigate to the directory where you want to create your project. Run the following command to create a new Django project:

django-admin startproject mywebsite

Replace “mywebsite” with your preferred project name.

Step 2: Create a Django App:

Navigate into your project directory and create a Django app. Apps are modular components of a Django project that serve specific purposes.

cd mywebsite
python manage.py startapp myapp

Step 3: Define Models:

Open the models.py file in your app directory (myapp/models.py) and define the data models for your website. For example, if you are creating a blog, you might define a Post model.

from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.title

Step 4: Create Database Tables:

Run the following commands to create the database tables based on your models:

python manage.py makemigrations
python manage.py migrate

Step 5: Create Views and Templates:

Define views in your views.py file (myapp/views.py) and create corresponding HTML templates in the templates directory.

Step 6: Configure URLs:

Connect your views to URLs by defining URL patterns in the urls.py file of your app and project.

Step 7: Run the Development Server:

Start the development server and preview your website locally by running the following command:

bashCopy code
python manage.py runserver
Save to grepper

Visit http://127.0.0.1:8000/ in your browser to see your Django website in action!

Free Django Hosting

Conclusion:

Congratulations! You’ve successfully created a basic website using Django. This is just the beginning — Django offers a wide range of features for handling user authentication, static files, and more. Explore the official Django documentation to continue building and refining your website. Happy coding!

--

--