数据操作视图是对数据模型进行操作,如增、删、改,从而实现Django与数据库的数据交互。一共有4种视图类分别是FormView、CreateView、UpdateView和DeleteView。
视图类FormView的底层是由TemplateResponseMixin、ContextMixin和View组成。其具有视图类TemplateView的所有属性和方法,同时还新增了:
示例代码:
app的form.py
from django import forms
from .models import PersonInfo
class PersonInfoForm(forms.ModelForm):
class Meta:
model = PersonInfo
fields = '__all__'
app的urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('', index.as_view(), name='index'),
path('result', result, name='result')
]
app的views.py
from django.views.generic.edit import FormView
from .form import PersonInfoForm
from django.http import HttpResponse
def result(request):
return HttpResponse('Success')
class index(FormView):
initial = {'name': 'Betty', 'age': 20}
template_name = 'index.html'
success_url = '/result'
form_class = PersonInfoForm
extra_context = {'title': '人员信息表'}
index.html
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<body>
<h3>{{ title }}</h3>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="确定">
</form>
</body>
</html>
视图类CreateView是对模型新增数据的视图类,是在表单视图类FormView的基础上加以封装的。其底层依旧是基于TemplateResponseMixin、ContextMixin和View组成,整个设计共继承8个类,同样在这三个类基础上,还新增重写了一些属性和方法:
视图类CreateView虽然具备TemplateView、SingleObjectMixin和FormView的所有属性和方法,但运行方式有些变化,其中最大特点在于get_form_class()方法,它是通过判断属性form_class(来自FormMixin类)、fields(来自ModelFormMixin类)和model(来自SingleObjectMixin类),从而实现数据新增操作。
判断方法如下:
整体来说CreateView视图类有两种表单生成方式:
代码示例:
views.py(其余的代码沿用之间的)
from django.views.generic.edit import CreateView
from .form import PersonInfoForm
from .models import PersonInfo
from django.http import HttpResponse
def result(request):
return HttpResponse('Success')
class index(CreateView):
initial = {'name': 'Betty', 'age': 20}
template_name = 'index.html'
success_url = '/result'
# 表单生成方式一
# form_class = PersonInfoForm
# 表单生成方式二
model = PersonInfo
# fields设置模型字段,从而生成表单字段
fields = ['name', 'age']
extra_context = {'title': '人员信息表'}
视图类UpdateView是在视图类FormView和DetailVIew的基础上实现的,它首先使用视图类DetailView的功能(SingleObjectMixin)通过路由变量查询数据表某条数据并显示在网页上,然后在视图类FormView的基础上通过表单方式实现数据修改。
代码示例:
app的urls.py
from django.urls import path
from .views import *
urlpatterns = [
# 定义路由
path('.html' , index.as_view(), name='index'),
path('result', result, name='result')
]
app的views.py
from django.views.generic.edit import UpdateView
from .models import PersonInfo
from django.http import HttpResponse
def result(request):
return HttpResponse('Success')
class index(UpdateView):
template_name = 'index.html'
success_url = '/result'
model = PersonInfo
# fields设置模型字段,从而生成表单字段
fields = ['name', 'age']
slug_url_kwarg = 'age'
slug_field = 'age'
context_object_name = 'personinfo'
extra_context = {'title': '人员信息表'}
index.html
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<body>
<h3>{{ title }}-{{ personinfo.name }}</h3>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="确定">
</form>
</body>
</html>
视图类DeleteView只能删除单条数据,路由变量为模型主键提供查询范围,查询出来的数据通过POST请求实现删除,删除过程有DeleteMixin的delete()方法完成。
代码示例:
app的urls.py
from django.urls import path
from .views import *
urlpatterns = [
# 定义路由
path('.html' , index.as_view(), name='index'),
path('result', result, name='result')
]
app的views.py
from django.views.generic.edit import DeleteView
from .models import PersonInfo
from django.http import HttpResponse
def result(request):
return HttpResponse('Success')
class index(DeleteView):
template_name = 'index.html'
success_url = '/result'
model = PersonInfo
context_object_name = 'personinfo'
extra_context = {'title': '人员信息表'}
HTML文件
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<body>
<h3>{{ title }}-{{ personinfo.name }}</h3>
<form method="post">
{% csrf_token %}
<div>删除{{ personinfo.name }}?</div>
<input type="submit" value="确定">
</form>
</body>
</html>