
FormView底层源码:
#get请求: 渲染表单
#post请求:数据校验失败后可以进行重定向 到 新的页面
# FormView继承了TemplateResponseMixin, BaseFormView)
class FormView(TemplateResponseMixin, BaseFormView):
#对于 TemplateResponseMixin
template_name = None
template_engine = None
response_class = TemplateResponse
content_type = None
#进行渲染的函数
def render_to_response(self, context, **response_kwargs):
#获得渲染模板html的路径
def get_template_names(self):
#对于BaseFormView
class BaseFormView(FormMixin, ProcessFormView):
class FormMixin(ContextMixin): #和上方的TemplateResponseMixin 作用差不多
完整示例:
1.创建并配置应用目录:
2.url.py 文件
from 应用目录名.views import thanks, SomeTingView
from django.urls import path
urlpatterns = [
path('something/',SomeTingView.as_view() ,name = 'something_form'),
path('thanks/',thanks )
]
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
from django.views.generic import FormView
from 应用目录.forms import SomeThingForm
class SomeTingView(FormView):
success_url = '/the_16/thanks' # 再跳转到的 函数 不要忘记/应用目录名/方法名
template_name = 'the_16/something.html' #定义要建立的html模板的名字
form_class = SomeThingForm # 获得表单
def thanks(request) :
return HttpResponse('hello world')
from django.db import models
# Create your models here.
class SomeThing(models.Model):
msg = models.CharField(max_length=100)
is_delete = models.BooleanField()
5.forms.py文件
from django import forms
from 应用目录.models import SomeThing
class SomeThingForm(forms.ModelForm):
class Meta:
model = SomeThing
fields = '__all__'
6.模板html
Title
注:虽然 views.py文件中没有写 form, 但是 只要 在html文件中使用{{ form }}, 就能够在前台网页渲染出 表单里的字段 , 提交后跳转到 success_url 设置的新的页面
CreateForm 功能:在views.py文件中,自动建表单存入数据库
分析:也就是说不需要在写字段保存的一些代码,它会直接保存到数据库对应的模型表里面.
使用:
success_url
form_class
model
def form_valid(self, form): #这个函数实现了createform的表单保存到数据库的功能.
"""If the form is valid, save the associated model."""
self.object = form.save()
return super().form_valid(form)
创建的html模板名字为:
1.模型名小写_form.
2.也可以 使用template_name = '' 来自己定义模型名
示例:
views.py文件
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
from django.views.generic import CreateView
from the_16.forms import SomeThingForm
def thanks(request) :
return HttpResponse('hello world')
class SometThingCreateView(CreateView):
success_url = '/应用目录名/thanks'
form_class = SomeThingForm
model = SomeThing #如果不引入模型,那么会报错,找不到 template_name 和 get_template
#如果一切正常,可能是 没有表, 可能是因为没有数据的迁移
url文件:
from 应用目录名.views import hello, thanks, SomeTingView,SometThingCreateView
from django.urls import path
urlpatterns = [
path('somtethingcreateview/',SometThingCreateView.as_view() ,name = 'create_form'),
path('thanks/',thanks )
]
models.py文件:
同上方的一样: 略
form.py文件
同上方的一样
html文件:
Title
UpdateView
功能:更新数据
注:一定要有数据才能使用 updateview, 否则会报错—>没有匹配数据
分析:#起到主要作用的是底层的 def get_object: 它将数据查询出来,然后返回object,进而进行修改
update提供的功能:修改数据的功能
数据从那里来:queryset pk参数
提供的模板变量: form
渲染的模板页命名:
1.template_name
2.模型名小写_form,html
表单从那里来: form_class
模型类从哪里来: model quert set
重定向的ur1是什么 success_url = '/应用目录名/要跳转的url'
示例
其他都和FormView一样,主要urls.py里面讲究
views.py文件主要部分:
class SometThingUpdateView(UpdateView):
success_url = '/应用目录名/thanks'
form_class = SomeThingForm # 表单
queryset = SomeThing.objects.all() #也可以用model = 模型名表单
urls.py 文件:
from the_16.views import hello, thanks, SomeTingView,SometThingUpdateView
from django.urls import path
urlpatterns = [
path('something/',SomeTingView.as_view() ,name = 'something_form'),
path('something/updateview/',SometThingUpdateView.as_view() ,name = 'something_form'),
path('thanks/',thanks )
]
# 也就是说 updateview 使用个表单一定是其他视图的表单, name .
# pk 是要自己在浏览器上方输入的. 作用---------->指定了要修改的那一条数据
DeleteView
功能:
删除数据
分析:#底层有 def delete: 这个方法提供了 self.object.delete() ,从而实现数据的删除.
def delete(self, request, *args, **kwargs):
"""
Call the delete() method on the fetched object and then redirect to the
success URL.
"""
self.object = self.get_object()
success_url = self.get_success_url()
self.object.delete()
return HttpResponseRedirect(success_url)
# update提供的功能:修改数据的功能
数据从那里来:queryset pk参数 一定要在urls.py 文件的 path中指明pk, 这样好删除数据
提供的模板变量: form
渲染的模板页命名:
1.template_name
2.模型名小写_form,html
表单从那里来: form_class
模型类从哪里来: model quert set
重定向的ur1是什么 success_url = '/应用目录名/要跳转的url'
示例:
DeleteView 和 UpdateView 使用方法相似.