TL16C752CI-Q1 UART芯片深度解析:64字节FIFO、自动流控制与中断机制实战
2026/7/25 18:46:17
Django 是一个高级的 Python Web 框架,以“快速开发、干净实用的设计”为核心理念。它由经验丰富的开发者构建,旨在减少 Web 开发中的重复性工作,让你专注于业务逻辑本身,而不是底层基础设施。
Ridiculously Fast(极速开发)
Reassuringly Secure(安全可靠)
Exceedingly Scalable(高度可扩展)
Django 采用Model-View-Template (MVT)架构:
| 组件 | 职责 |
|---|---|
| Model | 定义数据结构,与数据库交互(通过 ORM) |
| View | 处理请求逻辑,调用模型,返回响应 |
| Template | 渲染 HTML 页面,展示数据 |
💡 注意:Django 的 “View” 实际上相当于 MVC 中的 “Controller”。
# 1. 安装 Django pip install django # 2. 创建项目 django-admin startproject mysite # 3. 创建应用 cd mysite python manage.py startapp hello # 4. 编写视图(hello/views.py) from django.http import HttpResponse def index(request): return HttpResponse("Hello, Django!") # 5. 配置路由(mysite/urls.py) from django.urls import path from hello.views import index urlpatterns = [ path('hello/', index), ]启动服务器:
python manage.py runserver访问http://127.0.0.1:8000/hello/即可看到输出。
F()表达式:在数据库层面操作字段(避免竞态条件)Product.objects.update(price=F('price') * 1.05)Q()对象:构建复杂查询(支持 OR、NOT)Product.objects.filter(Q(name__icontains='笔记本') | Q(price__lt=5000))Subquery+OuterRef:实现跨表子查询async def视图Django 官方支持:
配置示例(settings.py):
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydb', 'USER': 'myuser', 'PASSWORD': 'mypass', 'HOST': 'localhost', 'PORT': '5432', } }MVT(Model-View-Template)是Django 框架采用的核心设计模式,它是对传统 MVC(Model-View-Controller)的一种变体。理解 MVT 是掌握 Django 开发的关键。
在标准的MVC中:
而在Django 的 MVT中:
✅ 简单记:Django 的 “View” 其实是 “Controller”,而 “Template” 才是真正的 “View”。
models.py中# models.py from django.db import models class Article(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True)✅ 无需写 SQL,Django 自动为你生成表结构(通过
makemigrations和migrate)。
request)HttpResponse、渲染模板等)views.py中# views.py from django.shortcuts import render from .models import Article def article_list(request): articles = Article.objects.all() return render(request, 'articles/list.html', {'articles': articles})⚠️ 注意:这里的 “View” 不是页面,而是处理请求的函数或类!
templates/目录下<!-- templates/articles/list.html --> <h1>文章列表</h1> <ul> {% for article in articles %} <li>{{ article.title }} ({{ article.created_at|date:"Y-m-d" }})</li> {% endfor %} </ul>✅ 模板只负责“展示”,不包含复杂逻辑(符合关注点分离原则)。
/articles/urls.py)将路径映射到视图函数:# urls.py from . import views urlpatterns = [ path('articles/', views.article_list, name='article_list'), ]Article模型myproject/ ├── myapp/ │ ├── models.py ← Model │ ├── views.py ← View │ ├── urls.py ← 路由(连接 URL 和 View) │ └── templates/ │ └── myapp/ │ └── list.html ← Template └── settings.pyrender()+ 模板分离展示。get_summary())。