[toc]
学习资料
《Django Web应用开发实战》
1. 数据操作视图
数据操作视图是对模型,如增、删、改,从而实现Django与数据库的数据交互
2. 表单视图 FormView
使用内置的表单功能,通过表单实现数据验证,响应输出等功能,用于显示表单数据。
在index/form.py写入
from django import formsfrom index.models import PersonInfoclass PersonInfoForm(forms.ModelForm):class Meta:model = PersonInfofields = \'__all__\'
index/urls.py新加路由
path(\'index_form/\', views.IndexForm.as_view(), name=\'index_form\'),path(\'result\', views.result, name=\'result\'),
index/views.py添加result视图函数
from django.views.generic import FormViewfrom index.form import PersonInfoFormdef result(request):return HttpResponse(\'success\')class IndexForm(FormView):# 设置表单初始化的数据initial = {\'name\': \'zy7y\', \'age\': 19}template_name = \'index.html\'# 设置重定向的路由地址success_url = \'/result\'# 设置表单类form_class = PersonInfoFormextra_context = {\'title\': \'人员信息表\'}
templates/index.html修改如下
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>{{ title }}</title></head><body><h3>title : {{ title }}</h3><form method="post">{% csrf_token %}{# 调用form表单 #}{{ form.as_p }}<input type="submit" value="确定"></form><br></body></html>
启动服务访问:
127.0.0.1:8000/index_form
,看到如下表单内容是由FormView渲染的

3. 新增视图 CreateView
是对模型新增数据的视图类,是在FormView的基础上加以封装的,简单说就是在FormView上面加了个数据新增的功能
index/views.py添加如下类视图
from django.views.generic import CreateViewfrom index.models import PersonInfoclass IndexCreate(CreateView):initial = {\'name\': \'qy\', \'age\': 23}template_name = \'index.html\'success_url = \'/result\'# 表单生成方式1# form_class = PersonInfoForm# 表单生成方式2model = PersonInfo# fields设置模型字段,从而生成表单字段fields = [\'name\', \'age\']extra_context = {\'title\': \'人员信息表\'}
index/urls.py添加以下路由
path(\'index_create/\', views.IndexCreate.as_view(), name=\'index_create\'),
启动服务访问
127.0.0.1:8000/index_create/


点击确定之后,数据库新增了该数据

4. 修改视图 UpdateView
在FormView和DetailView的基础上实现
index/urls.py添加以下路由
path(\'<age>.html\', views.IndexUpdate.as_view(), name=\'index_update\')
index/views.py添加以下视图
from django.views.generic import UpdateViewfrom index.models import PersonInfoclass IndexUpdate(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\': \'人员信息表\'}
templates/index.html修改如下
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>{{ title }}</title></head><body><h3>title : {{ title }}- {{ personinfo.name }}</h3><form method="post">{% csrf_token %}{# 调用form表单 #}{{ form.as_p }}<input type="submit" value="确定"></form><br></body></html>
**启动服务访问:http://127.0.0.1:8000/13.html **

5. 删除视图 DeleteView
只能删除单条数据,路由变量为模型主键提供查询范围,因为模型主键具有唯一性
index/urls.py添加如下路由,并且把UpdateView添加的路由注释掉,不然会出现一直使用的是
<age>.html
路由
path(\'<pk>.html\', views.IndexDelete.as_view(), name=\'index_delete\')
index/views.py添加如下视图
from django.views.generic import DeleteViewfrom index.models import PersonInfoclass IndexDelete(DeleteView):template_name = \'index.html\'success_url = \'/result\'model = PersonInfocontext_object_name = \'personinfo\'extra_context = {\'title\': \'人员信息表\'}
templates/index.html修改如下
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>{{ title }}</title></head><body><h3>title : {{ title }}- {{ personinfo.name }}</h3><form method="post">{% csrf_token %}<div>删除 {{ personinfo.name }}?</div><input type="submit" value="确定"></form><br></body></html>
启动服务访问路由:
127.0.0.1:8000/2.html

爱站程序员基地


