Django Beginner's Guide: From Basics to Building Complex Projects
Django Beginner's Guide: From Basics to Building Complex Projects
Django is a Python framework for rapid development of efficient, clear, and scalable web applications. This article will guide you from the basics of Django to learning how to build complex projects, helping you gradually advance in development.
1. Introduction to Django
Django is a "batteries-included" framework, which means it has many out-of-the-box features that help developers quickly set up and launch web applications. The advantages of Django include:
- Rapid Development: Provides rich features and a set of tools to help developers reduce repetitive work.
- Security: Built-in various security features to protect web applications from common attacks.
- Scalability: Suitable for applications of all sizes, from small websites to large complex systems.
2. Environment Setup
-
Install Python and pip: Django is based on Python, so first ensure that Python (recommended version 3.6 and above) is installed on your computer. You can check if Python is installed successfully with the following command:
python --versionIf Python is not installed, please visit Python official website to download and install it.
-
Install Django: Use pip to install Django:
pip install DjangoAfter installation, you can run the following command to verify if Django is successfully installed:
python -m django --version
3. Create Your First Django Project
-
Create a Project: Use the command-line tool provided by Django to create a new project. We will create a project named
myproject:django-admin startproject myprojectEnter the project directory:
cd myproject -
Start the Development Server: Run the following command to start Django's development server:
python manage.py runserverOpen your browser and visit
http://127.0.0.1:8000/, you should see Django's welcome page.
4. Create an Application
In Django, a project consists of multiple applications. Each application is an independent functional module.
-
Create an Application: In the project directory, run the following command to create a new application, for example named
blog:python manage.py startapp blog -
Register the Application: In the project's
settings.pyfile, add the newly created application to theINSTALLED_APPSlist:INSTALLED_APPS = [ ... 'blog', ]
5. Build Basic Models
Database models are the data structures in Django, usually closely related to the functionality of the application. We will take a simple blog application as an example.
-
Define Models: Define our models in the
blog/models.pyfile:from django.db import models class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title -
Migrate the Database: Run the following commands in the terminal to create the database tables:
python manage.py makemigrations python manage.py migrate
6. Create Views and URL Routing
Views are functions that handle requests and return responses, while URL routing assigns requests to the corresponding views.
-
Create Views: Add a simple view in the
blog/views.pyfile:from django.shortcuts import render from .models import Post def post_list(request): posts = Post.objects.all() return render(request, 'blog/post_list.html', {'posts': posts}) -
Configure URL Routing: Create a
urls.pyfile in theblogdirectory and configure the URL routing:from django.urls import path from . import views urlpatterns = [ path('', views.post_list, name='post_list'), ]Then include this application's URLs in the main project's
urls.py:from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog.urls')), ]
7. Create Templates
Templates are HTML files used to render data. In the blog directory, create a folder named templates/blog and create a post_list.html file within it.
Blog Posts
# Blog Posts
{% for post in posts %}
- {{ post.title }} ({{ post.created_at }})
{% endfor %}
8. Summary
Django is a powerful web development framework suitable for rapidly building efficient web applications. This article introduced the basic processes of environment setup, project creation, applications, models, views, and templates. As you delve deeper into your learning, you can try more Django features, such as user authentication, RESTful APIs, and integration with front-end frameworks (like React).
Continue building your projects, challenge complex functionalities, and enhance your development skills!




