Django UpdateView视图

UpdateView是Django中的一个通用视图,用于处理对象的更新操作。它允许用户更新一个已经存在的对象。UpdateView通常与一个模型表单一起使用,这样用户就可以看到当前对象的值,并可以修改它们。

1,添加视图

Test/app3/views.py

from django.shortcuts import render

# Create your views here.
from .models import Book

from django.views.generic import ListView
class BookListView(ListView):
    model = Book
    context_object_name = 'books'
    template_name = 'books/book_list.html'
    paginate_by = 10 # 设置展示页数数据


from django.views.generic import DetailView
class BookDetailView(DetailView):
    model = Book
    context_object_name = 'book'
    template_name = 'books/book_detail.html'


from django.views.generic.edit import CreateView
class BookCreateView(CreateView):
    model = Book
    template_name = 'books/book_form.html'
    fields = ['title', 'author', 'publication_date']
    success_url = '/app3/books/' # 重定向至书本列表路由地址

from django.urls import reverse_lazy
from django.views.generic.edit import UpdateView
class BookUpdateView(UpdateView):
    model = Book
    fields = ['title', 'author', 'publication_date']
    template_name = 'books/book_edit.html'
    success_url = reverse_lazy('book_list')

2,添加路由地址

Test/app3/urls.py

from django.urls import path
from . import views

from .views import BookListView
from .views import BookDetailView
from .views import BookCreateView
from .views import BookUpdateView

urlpatterns = [
    path('books/', BookListView.as_view(), name='book_list'),
    path('books/<int:pk>/', BookDetailView.as_view(), name='book_detail'),
    path('books/new/', BookCreateView.as_view(), name='book_new'),
    path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='BookUpdateView'),

]

3,添加html代码

Test/templates/books/book_edit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">更新</button>
</form>

</body>
</html>

4,访问页面

Test/templates/books/book_edit.html

http://127.0.0.1:8000/app3/books/1/edit/

相关推荐

  1. 视图

    2024-06-12 01:54:04       19 阅读
  2. Django视图

    2024-06-12 01:54:04       30 阅读
  3. 12.<span style='color:red;'>视图</span>

    12.视图

    2024-06-12 01:54:04      28 阅读
  4. MySQL-视图

    2024-06-12 01:54:04       35 阅读
  5. MySQL视图

    2024-06-12 01:54:04       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 01:54:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 01:54:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 01:54:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 01:54:04       18 阅读

热门阅读

  1. Android应用图标到应用显示过程

    2024-06-12 01:54:04       13 阅读
  2. element-plus ui的使用说明

    2024-06-12 01:54:04       14 阅读
  3. 数据分析------统计学知识点(四)

    2024-06-12 01:54:04       11 阅读
  4. C++构建MVC学生信息管理系统

    2024-06-12 01:54:04       10 阅读
  5. GIT生成SSH公钥图文教程

    2024-06-12 01:54:04       14 阅读
  6. SSID简介

    2024-06-12 01:54:04       9 阅读
  7. Web前端开发缺点:深入剖析与反思

    2024-06-12 01:54:04       9 阅读
  8. vue调用百度api时跨域问题的解决方案

    2024-06-12 01:54:04       8 阅读
  9. Django自定义CSS

    2024-06-12 01:54:04       6 阅读
  10. python连接mysql数据库、FastAPI、mysql-connector-python

    2024-06-12 01:54:04       7 阅读