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>
相关推荐
测试199824 分钟前
软件测试 - 单元测试总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
Mahir083 小时前
Spring 循环依赖深度解密:从问题本质到三级缓存源码级解析
java·后端·spring·缓存·面试·循环依赖·三级缓存
曲幽3 小时前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
前端若水4 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
涛声依旧-底层原理研究所5 小时前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
csdn_aspnet5 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
fantasy_arch6 小时前
pytorch人脸匹配模型
人工智能·pytorch·python
熊猫_豆豆6 小时前
广义相对论水星近日点进动完整详细数学推导
python·天体·广义相对论
web3.08889996 小时前
1688 图搜接口(item_search_img / 拍立淘) 接入方法
开发语言·python
AI算法沐枫6 小时前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归