两学一做网站安徽省,搭建漏洞网站,wordpress发布文章关键词,做网站还是小程序UpdateView是Django中的一个通用视图#xff0c;用于处理对象的更新操作。它允许用户更新一个已经存在的对象。UpdateView通常与一个模型表单一起使用#xff0c;这样用户就可以看到当前对象的值#xff0c;并可以修改它们。 1#xff0c;添加视图
Test/app3/views.py
fr…UpdateView是Django中的一个通用视图用于处理对象的更新操作。它允许用户更新一个已经存在的对象。UpdateView通常与一个模型表单一起使用这样用户就可以看到当前对象的值并可以修改它们。 1添加视图
Test/app3/views.py
from django.shortcuts import render# Create your views here.
from .models import Bookfrom django.views.generic import ListView
class BookListView(ListView):model Bookcontext_object_name bookstemplate_name books/book_list.htmlpaginate_by 10 # 设置展示页数数据from django.views.generic import DetailView
class BookDetailView(DetailView):model Bookcontext_object_name booktemplate_name books/book_detail.htmlfrom django.views.generic.edit import CreateView
class BookCreateView(CreateView):model Booktemplate_name books/book_form.htmlfields [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 Bookfields [title, author, publication_date]template_name books/book_edit.htmlsuccess_url reverse_lazy(book_list)2添加路由地址
Test/app3/urls.py
from django.urls import path
from . import viewsfrom .views import BookListView
from .views import BookDetailView
from .views import BookCreateView
from .views import BookUpdateViewurlpatterns [path(books/, BookListView.as_view(), namebook_list),path(books/int:pk/, BookDetailView.as_view(), namebook_detail),path(books/new/, BookCreateView.as_view(), namebook_new),path(books/int:pk/edit/, BookUpdateView.as_view(), nameBookUpdateView),] 3添加html代码
Test/templates/books/book_edit.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
bodyform methodPOST{% csrf_token %}{{ form.as_p }}button typesubmit更新/button
/form/body
/html 4访问页面
Test/templates/books/book_edit.html
http://127.0.0.1:8000/app3/books/1/edit/