马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
ListView、DetailView、CreateView、UpdateView 和 DeleteView 是 Django 框架中基于类的通用视图(Class-Based Generic Views)
设置 URL 路由
在 urls.py 中为这些视图设置路由:
- 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
- 用途: 表现一组对象的列表。
- 典范场景: 展示数据库中的多条记载,比如博客文章列表、用户列表等。
- 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' # 自定义上下文变量名
复制代码- <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
- 用途: 表现单个对象的具体信息。
- 典范场景: 检察某篇文章的具体内容、某个用户的个人资料等。
- # 显示单篇文章的详细信息
- class PostDetailView(DetailView):
- model = Post
- template_name = 'blog/post_detail.html' # 自定义模板路径
- context_object_name = 'post' # 自定义上下文变量名
复制代码- <h1>{{ post.title }}</h1>
- <p>{{ post.content }}</p>
- <p>发布时间: {{ post.created_at }}</p>
- <a href="{% url 'post-list' %}">返回列表</a>
复制代码
3. CreateView
- 用途: 创建一个新的对象。
- 典范场景: 提供一个表单让用户填写并提交数据,例如发布一篇新文章。
- # 创建新文章
- class PostCreateView(CreateView):
- model = Post
- template_name = 'blog/post_form.html' # 自定义模板路径
- fields = ['title', 'content'] # 表单中需要显示的字段
- success_url = reverse_lazy('post-list') # 成功后跳转到文章列表页
复制代码- <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
- 用途: 更新一个现有的对象。
- 典范场景: 编辑已有的数据,例如修改一篇文章的内容。
- # 更新现有文章
- class PostUpdateView(UpdateView):
- model = Post
- template_name = 'blog/post_form.html' # 自定义模板路径
- fields = ['title', 'content'] # 表单中需要显示的字段
- success_url = reverse_lazy('post-list') # 成功后跳转到文章列表页
复制代码 5. DeleteView
- 用途: 删除一个现有的对象。
- 典范场景: 删除某篇文章、某个用户等。
- # 删除文章
- class PostDeleteView(DeleteView):
- model = Post
- template_name = 'blog/post_confirm_delete.html' # 自定义模板路径
- success_url = reverse_lazy('post-list') # 成功后跳转到文章列表页
复制代码
- <h1>确认删除文章 "{{ object.title }}" 吗?</h1>
- <form method="post">
- {% csrf_token %}
- <button type="submit">确认删除</button>
- </form>
- <a href="{% url 'post-list' %}">取消</a>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |