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>
相关推荐
whcyhhh23 分钟前
头歌实践教学平台:大数据存储2023(六)
大数据·数据库·python
for_ever_love__1 小时前
python基础语法学习: 闭包
开发语言·python·学习·闭包
ly76891 小时前
Python 全面入门:从核心语法到工程实践
开发语言·python
why技术1 小时前
eli5,我觉得这个全网在吹的技能,使用体验真的很一般啊。
前端·人工智能·后端
学习星球1 小时前
Qwik 框架入门实战:从开源项目 Qwik City 开始,用可恢复性替代水合
后端·前端框架·开源·c5全栈
Niuguangshuo3 小时前
DeepFilterNet 3:端侧实时全带降噪的开源标杆
python
IT_陈寒3 小时前
被Java的final坑惨了,这些细节你可能也忽略了
前端·人工智能·后端
第一程序员3 小时前
AI 辅助编程的 7 个误区:把模型当高级搜索引擎是对它的最大浪费
python·rust·github
Zadig3 小时前
企业 Agent 总烂在 Demo 里?Zadig 工作流 AI 任务给了一条路
后端·devops
Zadig4 小时前
告别"人肉扛雷":Zadig 用 AI 接管发布前最脏最累的 15 分钟
后端·aiops