The first time I dabbled in web coding with Django, it took me a whole week just to run the damn "Hello World" server. The problem is not that Django is difficult, but the lack of a "standard" roadmap for newcomers. Many of you wonder where to start learning Django yourself so you don't get overwhelmed by dozens of documents. This article will take you from scratch, set up the environment, create your first project, and understand the MVT structure in just one afternoon. Of course, to keep up, you need to master the language foundation first, if not then Learning basic Python for beginners is an important first step. I promise, after reading this article, you will confidently use Python Django to create web applications from scratch in the smoothest way.
First step: Build the skeleton for the web app in just 15 minutes
To start creating a web app with Django for beginners, you only need 15 minutes to install Python, create a virtual environment, install the Django framework and create your first project.
At Pham Hai, I always advise newbies not to rush into logic code right away. Build a solid foundation first. As of March 2026, Django 6.0 version was released with many strong improvements in asynchronous processing performance (async), but the basic initialization steps remain the same core values. Below are the most accurate steps to create a Django project that professional programmers still apply every day.
Prepare the "airport": Install Python and virtual environment (Virtual Environment)
Virtual Environment helps isolate the libraries of the current Django project from the system, avoiding version conflicts when you work on multiple projects at the same time.
Trước tiên, bạn cần chắc chắn máy tính đã cài Python (khuyên dùng bản 3.13 hoặc 3.14 mới nhất của năm 2026). Sau đó, mở terminal (hoặc Command Prompt) lên và gõ lệnh tạo môi trường ảo:
python -m venv myenv
To enable it, use the following command:
- Trên Windows:
myenvScriptsactivate - Trên Mac/Linux:
source myenv/bin/activate
Khi thấy chữ (myenv) hiện ra ở đầu dòng lệnh, bạn đã thành công. Việc quản lý môi trường này trên máy cá nhân khá dễ dàng. Tuy nhiên, khi đưa web lên server thật sau này, môi trường sẽ phức tạp hơn rất nhiều. Do đó, việc trang bị thêm kiến thức Quản trị Linux server cơ bản cho developer là một lợi thế cực lớn giúp bạn tự tin triển khai dự án.
Lệnh thần chú quan trọng: pip install django và django-admin startproject
Lệnh pip install django dùng để tải framework về máy, còn django-admin startproject <tên_project> sẽ tự động sinh ra toàn bộ cấu trúc thư mục tiêu chuẩn cho ứng dụng web.
Đã có môi trường, giờ là lúc "rước" Web Framework này về. Bạn chỉ cần gõ:
pip install django
Hệ thống Pip sẽ tự động kéo bản Django mới nhất về máy. Tiếp theo, hãy khởi tạo project mang tên mywebapp bằng lệnh:
django-admin startproject mywebapp
Lúc này, một thư mục mới toanh tên mywebapp sẽ xuất hiện. Đây chính là bước đầu tiên trong hướng dẫn lập trình web Django từng bước. Thư mục này chứa mọi thứ để một Project Django có thể hoạt động.
| Basic command | Main function | Note for newbies |
|---|---|---|
pip install django |
Download the framework from the PyPI repository | Always run this command with the virtual environment enabled. |
django-admin startproject |
Initialize the root directory frame | Do not use spaces or special characters in the name. |
Chào thế giới: Chạy server với lệnh python manage.py runserver và thấy trang web đầu tiên của bạn.
Lệnh python manage.py runserver khởi động một Server phát triển (Development Server) cục bộ, giúp bạn xem trước website của mình trên trình duyệt qua địa chỉ localhost.
Hãy di chuyển vào thư mục vừa tạo bằng lệnh cd mywebapp. Sau đó, gõ dòng lệnh huyền thoại này:
python manage.py runserver
Mở trình duyệt và truy cập http://127.0.0.1:8000/. Bạn sẽ thấy biểu tượng tên lửa bay lên cùng dòng chữ báo hiệu cài đặt thành công. Cảm giác lúc này cực kỳ "yomost" đúng không? Bạn vừa chính thức bước vào thế giới lập trình web với một Framework mã nguồn mở hàng đầu thế giới.
Decoding the Django "matrix": What are each files and folders used for?
Một project Django mặc định bao gồm các file quan trọng như manage.py để điều khiển hệ thống, settings.py để cấu hình tổng thể và urls.py để phân luồng đường dẫn truy cập.
When opening the project folder with VS Code or PyCharm, many of you will be overwhelmed by a series of unfamiliar files. Don't worry, the process of learning Django to create a website requires you to clearly understand the nature of each file instead of learning it by rote. We will dissect the 3 most important "links".
manage.py: Người quản gia đắc lực của bạn
manage.py là một script Python đóng vai trò như một công cụ dòng lệnh, giúp bạn thực thi các tác vụ quản trị như chạy server, tạo database, hay tạo tài khoản admin.
Bạn sẽ gọi tên anh quản gia này liên tục trong suốt quá trình phát triển. Bất cứ khi nào cần giao tiếp với project, bạn đều dùng cú pháp python manage.py [lệnh]. Tuyệt đối không sửa đổi nội dung bên trong file này nhé. Cứ để nó làm đúng nhiệm vụ của một "người vận chuyển" mệnh lệnh từ bạn đến hệ thống lõi của Django.
settings.py: Bộ não trung tâm của toàn bộ ứng dụng
File settings.py chứa toàn bộ cấu hình của ứng dụng web, từ kết nối Cơ sở dữ liệu, ngôn ngữ, múi giờ, cho đến việc khai báo các app con (INSTALLED_APPS).
Mọi thiết lập sống còn đều nằm ở đây. Ví dụ, biến ALLOWED_HOSTS quyết định những tên miền nào được phép truy cập vào web của bạn. Khi đưa web lên mạng thật, bạn bắt buộc phải điền chính xác tên miền vào danh sách này để tránh lỗi bảo mật. Nếu bạn đang băn khoăn về việc chọn mua domain cho dự án sắp tới, hãy tham khảo ngay bài viết Mua tên miền ở đâu giá rẻ uy tín 2026 để có quyết định đúng đắn và tiết kiệm nhất.
urls.py: Người chỉ đường tận tuỵ, quyết định ai đi lối nào
urls.py chịu trách nhiệm URL Routing, nhận các yêu cầu đường dẫn từ người dùng (như /home, /about) và trỏ chúng đến đúng nơi xử lý (Views).
Cứ tưởng tượng file này như một tổng đài viên mẫn cán. Khách gọi đến nhánh số 1 (URL /tin-tuc/), tổng đài viên sẽ nối máy đến phòng ban xử lý tin tức. Việc cấu hình file urls.py gọn gàng, có tính logic cao sẽ giúp Ứng dụng web của bạn có những đường dẫn thân thiện, hỗ trợ cực tốt cho quá trình SEO sau này.
Let's start coding: Build your first feature with the MVT model
Django operates based on the MVT (Model-View-Template) architecture. Model manages data, View handles business logic, and Template takes care of displaying the user interface.
To build a web application with basic Django, you must master this MVT trio. At Pham Hai, I often compare it to a restaurant: Model is the ingredient warehouse, View is the processing chef, and Template is the beautifully presented dish on the table. This separation makes the code much cleaner and easier to maintain.
Models.py: Where to format data (Just imagine it like creating a table in Excel).
models.py là nơi bạn sử dụng code Python để định nghĩa cấu trúc của cơ sở dữ liệu, mỗi một class (lớp) tương ứng với một bảng trong database.
Instead of writing messy, error-prone SQL statements, you just need to simply declare in Python:
from django.db import models
class BaiViet(models.Model):
tieu_de = models.CharField(max_length=200)
noi_dung = models.TextField()
ngay_tao = models.DateTimeField(auto_now_add=True)
After declaring, Django will automatically understand and create a table with the corresponding columns. This is an indispensable core step when creating a website with Django.
| Field Type | Practical application | Declaration example |
|---|---|---|
| CharField | Save short string (name, title) | models.CharField(max_length=100) |
| TextField | Save long text (content) | models.TextField() |
| DateTimeField | Save the date and time the post was created | models.DateTimeField(auto_now_add=True) |
Views.py: Where the logic, requests and results are returned (The magic happens here!).
views.py đóng vai trò trung gian, nhận yêu cầu từ URL, lấy dữ liệu từ Model (nếu cần), tính toán logic và gửi kết quả cuối cùng cho Template hiển thị.
Chef View will receive orders from customers. If the customer wants to see the list of articles, the View will run down to the repository (Model), take all the article data, re-cook it to standard format, and then throw it to the Template. Views can be written as functions (Function-based views) for beginners, or as classes (Class-based views) to reuse code when building web applications with Django Framework at large scale.
Templates: The "front" of the application (It's the beautiful HTML and CSS files).
Templates in Django are HTML files embedded with special tags (Template tags) to display dynamic data passed from Views.
This is where you can unleash your interface creativity, turning raw data streams into eye-catching websites. To do this part well, you must know Learn basic HTML and CSS for beginners to layout the page properly. Furthermore, if you want the website to have dynamic effects, such as color-changing buttons or smooth popups without reloading the page, adding knowledge Learn basic JavaScript for beginners 2026 is a must.
Connecting it all: URL Routing for beginners.
URL Routing là quá trình liên kết một chuỗi URL cụ thể trong file urls.py với một hàm xử lý tương ứng trong file views.py.
Bạn mở urls.py và thêm một dòng đơn giản: path('bai-viet/', views.hien_thi_bai_viet). Vậy là xong! Chu trình MVT đã khép kín hoàn toàn. Khách gõ URL -> urls.py bắt được đường dẫn -> gọi Views xử lý -> Views lấy dữ liệu từ Models -> Views ném vào Templates -> Trả về giao diện cho trình duyệt. Rất logic và chặt chẽ đúng không?
Django's "secret weapons" that newbies need to know
Django stands out due to its batteries-included features such as automated Admin Interface, powerful ORM, and the ability to easily convert Database types.
The reason Django is trusted by giants like Instagram, Pinterest or Spotify is because it provides extremely powerful tools. These "weapons" help programmers save weeks of hand-coding repetitive features.
Available admin interface: Manage data without one-line code.
Django's Admin Interface is a content management system (CMS) that is automatically created based on the Models you have defined, allowing you to add, edit, and delete data instantly.
Chỉ với lệnh python manage.py createsuperuser, bạn đã tự cấp cho mình một tài khoản quyền lực. Đăng nhập vào đường dẫn /admin, một giao diện quản lý xịn sò hiện ra. Bạn có thể thêm bài viết mới, quản lý thông tin người dùng mà chưa cần phải tự tay thiết kế bất kỳ form nhập liệu HTML nào. Đây là điểm ăn tiền nhất của Django so với các framework khác.
Divine ORM: Talk to the database with Python instead of dry SQL.
ORM (Object-Relational Mapping) in Django allows you to interact with the database using pure Python methods, safely automatically translating them into corresponding SQL statements.
Ví dụ, muốn lấy tất cả bài viết, thay vì viết câu lệnh SELECT * FROM BaiViet dễ gây nhầm lẫn, bạn chỉ cần gõ BaiViet.objects.all(). ORM không chỉ giúp code ngắn gọn, dễ bảo trì mà còn bảo mật cực tốt. Nó tự động escape các ký tự đặc biệt, giúp ứng dụng của bạn miễn nhiễm hoàn toàn với các cuộc tấn công SQL Injection nguy hiểm.
Connecting to a "real" Database (PostgreSQL/MySQL): Elevate your project
Mặc định Django dùng SQLite cho môi trường phát triển, nhưng bạn có thể dễ dàng cấu hình lại file settings.py để kết nối Django với cơ sở dữ liệu lớn hơn như PostgreSQL hay MySQL cho môi trường thực tế.
When working on a real project with a large amount of traffic, tiny SQLite is not enough. Switching to PostgreSQL on Django is incredibly smooth with just a few lines of configuration. Of course, when the database swells and requires high processing speed, you will have to consider configuring a professional hosting server. At that time, understanding Shared hosting vs VPS vs Dedicated will help you optimize costs and choose the most stable database running environment.
See, from an empty folder, you have personally built a web app with a complete and professional structure. This is the solid foundation for your programming career. The road ahead is still very long, you will still get familiar with RESTful API, payment processing, but just be proud of what you have done today. When preparing to bring your website to the internet, you will need to pay closer attention to security and servers. Learning What is SSL HTTPS and how to install it for websites to encrypt user data, as well as understanding Nginx vs Apache comparing web server 2026 to configure proxy are the next steps that cannot be ignored. Everything great starts from these small lines of code.
If you have any questions during the practice process, don't hesitate to comment below, I will help answer as soon as possible. What topic would you like me to write about next in this Django tutorial series?
Note: The information in this article is for reference only. For the best advice, please contact us directly for specific advice based on your actual needs.