If you’re new to web development and want to build powerful, scalable web applications quickly, Django is one of the best frameworks to start with. It’s written in Python—one of the most beginner-friendly programming languages—and comes with a wide range of built-in features that make web development fast, secure, and efficient.
In this Django tutorial, we’ll walk through the basics of Django and help you get started with your first Django project. Whether you're a coding enthusiast or completely new to backend development, this guide is tailored to ease your learning curve.
What is Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Created in 2005, Django was built by developers who wanted a framework that could take care of much of the hassle of web development, so you can focus on writing your app without reinventing the wheel.
Some key features of Django include:
Fast development: Built-in tools for routing, templating, authentication, and admin interfaces.
Security: Protects against common threats like SQL injection, XSS, and CSRF attacks.
Scalability: Used by large websites like Instagram, Pinterest, and Mozilla.
Prerequisites
Before jumping in, make sure you have:
Basic knowledge of Python
Python installed on your computer (preferably version 3.7+)
A code editor like VSCode or PyCharm
Pip (Python package manager)
Once you’re ready, let’s set up Django.
Step 1: Setting Up Django
First, open your terminal or command prompt and create a virtual environment. This keeps your project dependencies isolated.
python -m venv env source env/bin/activate # On Windows: envScriptsactivate
Next, install Django using pip:
pip install django
To verify the installation:
django-admin --version
Step 2: Creating Your First Django Project
Create a new Django project by running:
django-admin startproject mysite cd mysite
This will generate a basic project structure:
manage.py
: A command-line tool to manage your project.mysite/
: The main project folder containing settings and URLs.
To run your server:
python manage.py runserver
Open your browser and go to http://127.0.0.1:8000/
. You should see the Django welcome page!
Step 3: Creating a Django App
In Django, a project can contain multiple apps. Let’s create one called “blog”.
python manage.py startapp blog
This creates another folder with files for views, models, and more.
Next, add the app to your project. Open mysite/settings.py
and add 'blog',
to the INSTALLED_APPS
list.
Step 4: Writing Your First View
Now, let’s create a simple view. In blog/views.py
, add:
from django.http import HttpResponse
def home(request): return HttpResponse("Welcome to my blog!")
To display this view in your browser, connect it to a URL. Create a file called urls.py
inside the blog
folder and add:
from django.urls import path from . import views
urlpatterns = [ path('', views.home, name='home'), ]
Then, link it to the main URL config in mysite/urls.py
:
from django.contrib import admin from django.urls import include, path
urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog.urls')), ]
Restart the server if needed, and navigate to http://127.0.0.1:8000/
—you should see your “Welcome to my blog!” message.
Step 5: Creating a Model
Let’s add a simple blog post model. In blog/models.py
:
from django.db import models
class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title
To apply this model to your database:
python manage.py makemigrations python manage.py migrate
Step 6: Using the Admin Panel
Django comes with a powerful admin interface. First, create a superuser:
python manage.py createsuperuser
Then, register your model in blog/admin.py
:
from django.contrib import admin from .models import Post
admin.site.register(Post)
Now go to http://127.0.0.1:8000/admin/
, log in, and you can add, edit, or delete blog posts using a graphical interface.
Final Thoughts
And there you have it—a working Django project with a blog app and admin panel!
While this tutorial only scratches the surface, you’ve already learned how to:
Set up Django
Create a project and app
Build and display views
Use models and the admin interface
From here, you can explore Django templates, forms, user authentication, and deploying your app to the web.
Django has a steep learning curve at first, but once you understand the basics, you’ll find it incredibly efficient and enjoyable. So stick with it, build projects, and don’t be afraid to break things as you learn!
0 Comments