Django视图(未分离)

ListView、DetailView、CreateView、UpdateView 和 DeleteView 是 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>
相关推荐
木子欢儿17 分钟前
Ubuntu 26.04 LTS 安装Python 3.12
linux·运维·服务器·python·ubuntu
Wellijohn32 分钟前
Python 异步并发:我把 5 个坑踩完了,你照着抄模板就行
python
AI创界者39 分钟前
Python 进阶实战(六十二章):分布式定时任务、异步工作流与幂等补偿机制实战
人工智能·python·aigc
会编程的吕洞宾1 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
技术小代1 小时前
弹窗里的 RegionName 为什么不生效?把区域作用域和模块化一次讲明白|MVVM框架-Prism-3
后端
斯维赤1 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
炸薯条!2 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
程序员cxuan2 小时前
上海团队开源了首个国产记忆模型,554B!这也太狠了
人工智能·后端·程序员
天天鸭3 小时前
前端转全栈,要学会后端看项目的思维
前端·后端·ai编程
BingoGo3 小时前
为什么 PHP 应该只保留 ZTS 构建模式
后端·php