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>
相关推荐
救救孩子把1 小时前
PyTorch 浮点数精度全景:从 float16/bfloat16 到 float64 及混合精度实战
人工智能·pytorch·python
意.远1 小时前
PyTorch数据操作基础教程:从张量创建到高级运算
人工智能·pytorch·python·深度学习·机器学习
明月看潮生3 小时前
青少年编程与数学 02-016 Python数据结构与算法 29课题、自然语言处理算法
python·算法·青少年编程·自然语言处理·编程与数学
西柚小萌新4 小时前
【Python爬虫基础篇】--1.基础概念
开发语言·爬虫·python
CPPAlien6 小时前
Python for MLOps - 第一阶段学习笔记
python
喵~来学编程啦6 小时前
【模块化编程】Python文件路径检查、跳转模块
开发语言·python
淬渊阁6 小时前
Hello world program of Go
开发语言·后端·golang
Pandaconda6 小时前
【新人系列】Golang 入门(十五):类型断言
开发语言·后端·面试·golang·go·断言·类型
周Echo周7 小时前
16、堆基础知识点和priority_queue的模拟实现
java·linux·c语言·开发语言·c++·后端·算法
黎明沐白7 小时前
Pytorch Hook 技巧
人工智能·pytorch·python