Django视图(未分离)

ListViewDetailViewCreateViewUpdateViewDeleteView 是 Django 框架中基于类的通用视图(Class-Based Generic Views)

配置 URL 路由

urls.py 中为这些视图配置路由:

python 复制代码
from django.urls import path
from .views import (
    PostListView,
    PostDetailView,
    PostCreateView,
    PostUpdateView,
    PostDeleteView,
)

urlpatterns = [
    path('', PostListView.as_view(), name='post-list'),  # 文章列表页
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),  # 文章详情页
    path('post/new/', PostCreateView.as_view(), name='post-create'),  # 创建新文章
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),  # 更新文章
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),  # 删除文章
]

1. ListView

  • 用途: 显示一组对象的列表。
  • 典型场景: 展示数据库中的多条记录,比如博客文章列表、用户列表等。
python 复制代码
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post

# 显示所有文章的列表
class PostListView(ListView):
    model = Post
    template_name = 'blog/post_list.html'  # 自定义模板路径
    context_object_name = 'posts'  # 自定义上下文变量名
html 复制代码
<h1>文章列表</h1>
<a href="{% url 'post-create' %}">新建文章</a>
<ul>
    {% for post in posts %}
        <li>
            <a href="{% url 'post-detail' post.pk %}">{{ post.title }}</a>
            <a href="{% url 'post-update' post.pk %}">编辑</a>
            <a href="{% url 'post-delete' post.pk %}">删除</a>
        </li>
    {% endfor %}
</ul>

2. DetailView

  • 用途: 显示单个对象的详细信息。
  • 典型场景: 查看某篇文章的详细内容、某个用户的个人资料等。
python 复制代码
# 显示单篇文章的详细信息
class PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post_detail.html'  # 自定义模板路径
    context_object_name = 'post'  # 自定义上下文变量名
html 复制代码
<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>
<p>发布时间: {{ post.created_at }}</p>
<a href="{% url 'post-list' %}">返回列表</a>

3. CreateView

  • 用途: 创建一个新的对象。
  • 典型场景: 提供一个表单让用户填写并提交数据,例如发布一篇新文章。
python 复制代码
# 创建新文章
class PostCreateView(CreateView):
    model = Post
    template_name = 'blog/post_form.html'  # 自定义模板路径
    fields = ['title', 'content']  # 表单中需要显示的字段
    success_url = reverse_lazy('post-list')  # 成功后跳转到文章列表页
html 复制代码
<h1>{% if object %}编辑文章{% else %}新建文章{% endif %}</h1>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">保存</button>
</form>
<a href="{% url 'post-list' %}">取消</a>

4. UpdateView

  • 用途: 更新一个现有的对象。
  • 典型场景: 编辑已有的数据,例如修改一篇文章的内容。
python 复制代码
# 更新现有文章
class PostUpdateView(UpdateView):
    model = Post
    template_name = 'blog/post_form.html'  # 自定义模板路径
    fields = ['title', 'content']  # 表单中需要显示的字段
    success_url = reverse_lazy('post-list')  # 成功后跳转到文章列表页

5. DeleteView

  • 用途: 删除一个现有的对象。
  • 典型场景: 删除某篇文章、某个用户等。
python 复制代码
# 删除文章
class PostDeleteView(DeleteView):
    model = Post
    template_name = 'blog/post_confirm_delete.html'  # 自定义模板路径
    success_url = reverse_lazy('post-list')  # 成功后跳转到文章列表页
html 复制代码
<h1>确认删除文章 "{{ object.title }}" 吗?</h1>
<form method="post">
    {% csrf_token %}
    <button type="submit">确认删除</button>
</form>
<a href="{% url 'post-list' %}">取消</a>
相关推荐
喵同志不止步于码农9 小时前
Java Caffeine 快速入门
java·开发语言·后端·并发·缓存一致性
Fluxproxy9 小时前
Python请求玄学根治:彻底解决脚本间歇性超时、断连、假死问题
网络·python·安全
青 春 记 忆9 小时前
零基础入门Python15|关联、聚合、索引与事务:订单数据库
开发语言·python·后端开发
东风破_9 小时前
NestJS CRUD 实战:用 Todos 模块打通 Controller-Service-Module 三层
后端
东风破_9 小时前
NestJS 框架入门:从工厂模式到模块化架构
后端
灯澜忆梦10 小时前
【基于GO的Web开发4】html/template 模板语法
后端·golang·html
一只叫煤球的猫10 小时前
Spring AI 2.0 源码解析(三):ChatClient 的 Fluent API 如何构建请求?
java·后端·面试
qq_5137280411 小时前
Alert 原生弹窗处理
python
掘金者阿豪11 小时前
数据库迁移工具选了半年,最后发现决策的起点错了
后端