django 模板视图,表单视图,各种视图
Generic editing views¶
The following views are described on this page and provide a foundation for editing content:
- django.views.generic.edit.FormView
- django.views.generic.edit.CreateView
- django.views.generic.edit.UpdateView
- django.views.generic.edit.DeleteView
Note
Some of the examples on this page assume that an Author model has been defined as follows inmyapp/models.py:
from django.core.urlresolvers import reverse
from django.db import models class Author(models.Model):
name = models.CharField(max_length=200) def get_absolute_url(self):
return reverse('author-detail', kwargs={'pk': self.pk})
FormView¶
- class django.views.generic.edit.FormView¶
-
A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseFormView
- django.views.generic.edit.FormMixin
- django.views.generic.edit.ProcessFormView
- django.views.generic.base.View
Example myapp/forms.py:
from django import forms class ContactForm(forms.Form):
name = forms.CharField()
message = forms.CharField(widget=forms.Textarea) def send_email(self):
# send email using the self.cleaned_data dictionary
passExample myapp/views.py:
from myapp.forms import ContactForm
from django.views.generic.edit import FormView class ContactView(FormView):
template_name = 'contact.html'
form_class = ContactForm
success_url = '/thanks/' def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
form.send_email()
return super(ContactView, self).form_valid(form)Example myapp/contact.html:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Send message" />
</form>
CreateView¶
- class django.views.generic.edit.CreateView¶
-
A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.detail.SingleObjectTemplateResponseMixin
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseCreateView
- django.views.generic.edit.ModelFormMixin
- django.views.generic.edit.FormMixin
- django.views.generic.detail.SingleObjectMixin
- django.views.generic.edit.ProcessFormView
- django.views.generic.base.View
Attributes
- template_name_suffix¶
-
The CreateView page displayed to a GET request uses a template_name_suffix of '_form'. For example, changing this attribute to '_create_form' for a view creating objects for the example Author model would cause the default template_name to be 'myapp/author_create_form.html'.
- object¶
-
When using CreateView you have access to self.object, which is the object being created. If the object hasn’t been created yet, the value will be None.
Example myapp/views.py:
from django.views.generic.edit import CreateView
from myapp.models import Author class AuthorCreate(CreateView):
model = Author
fields = ['name']Example myapp/author_form.html:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create" />
</form>
UpdateView¶
- class django.views.generic.edit.UpdateView¶
-
A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object. This uses a form automatically generated from the object’s model class (unless a form class is manually specified).
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.detail.SingleObjectTemplateResponseMixin
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseUpdateView
- django.views.generic.edit.ModelFormMixin
- django.views.generic.edit.FormMixin
- django.views.generic.detail.SingleObjectMixin
- django.views.generic.edit.ProcessFormView
- django.views.generic.base.View
Attributes
- template_name_suffix¶
-
The UpdateView page displayed to a GET request uses a template_name_suffix of '_form'. For example, changing this attribute to '_update_form' for a view updating objects for the example Author model would cause the default template_name to be 'myapp/author_update_form.html'.
- object¶
-
When using UpdateView you have access to self.object, which is the object being updated.
Example myapp/views.py:
from django.views.generic.edit import UpdateView
from myapp.models import Author class AuthorUpdate(UpdateView):
model = Author
fields = ['name']
template_name_suffix = '_update_form'Example myapp/author_update_form.html:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
</form>
DeleteView¶
- class django.views.generic.edit.DeleteView¶
-
A view that displays a confirmation page and deletes an existing object. The given object will only be deleted if the request method is POST. If this view is fetched via GET, it will display a confirmation page that should contain a form that POSTs to the same URL.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
- django.views.generic.detail.SingleObjectTemplateResponseMixin
- django.views.generic.base.TemplateResponseMixin
- django.views.generic.edit.BaseDeleteView
- django.views.generic.edit.DeletionMixin
- django.views.generic.detail.BaseDetailView
- django.views.generic.detail.SingleObjectMixin
- django.views.generic.base.View
Attributes
- template_name_suffix¶
-
The DeleteView page displayed to a GET request uses a template_name_suffix of '_confirm_delete'. For example, changing this attribute to '_check_delete' for a view deleting objects for the example Author model would cause the default template_name to be 'myapp/author_check_delete.html'.
Example myapp/views.py:
from django.views.generic.edit import DeleteView
from django.core.urlresolvers import reverse_lazy
from myapp.models import Author class AuthorDelete(DeleteView):
model = Author
success_url = reverse_lazy('author-list')Example myapp/author_confirm_delete.html:
<form action="" method="post">{% csrf_token %}
<p>Are you sure you want to delete "{{ object }}"?</p>
<input type="submit" value="Confirm" />
</form>
django 模板视图,表单视图,各种视图的更多相关文章
- 实验2、Flask模板、表单、视图和重定向示例
实验内容 1. 实验内容 表单功能与页面跳转功 能是Web应用程序的基础功能,学习并使用他们能够更好的完善应用程序的功能.Flask使用了名为Jinja2的模板引擎,该引擎根据用户的交互级别显示应用程 ...
- Django 11 form表单(状态保持session、form表单及注册实现)
Django 11 form表单(状态保持session.form表单及注册实现) 一.状态保持 session 状态保持 #1.http协议是无状态的:每次请求都是一次新的请求,不会记得之前通信的状 ...
- Django--分页器(paginator)、Django的用户认证、Django的FORM表单
分页器(paginator) >>> from django.core.paginator import Paginator >>> objects = ['joh ...
- Django中的表单
目录 表单 Django中的表单 用表单验证数据 自定义验证 表单 HTML中的表单是用来提交数据给服务器的,不管后台服务器用的是 Django 还是 PHP还是JSP还是其他语言.只要把 inpu ...
- 第二十二章 Django会话与表单验证
第二十二章 Django会话与表单验证 第一课 模板回顾 1.基本操作 def func(req): return render(req,'index.html',{'val':[1,2,3...]} ...
- 转载:Django之form表单
转载: 一.使用form类创建一个表单 先定义好一个RegForm类: forms.py from django import forms # 导入forms类 class NameForm(form ...
- Django的form表单
html的form表单 django中,前端如果要提交一些数据到views里面去,需要用到 html里面的form表单. 例如: # form2/urls.py from django.contrib ...
- Django:提交表单时遇到403错误:CSRF verification failed
Django:提交表单时遇到403错误:CSRF verification failed 问题: 提交表单时遇到403错误:CSRF verification failed 解决方案: 在表单界面ht ...
- Django使用普通表单、Form、以及modelForm操作数据库方式总结
Django使用普通表单.Form.以及modelForm操作数据库主要应用于增删该查的情景下,流程通用如下,只是实现方式不一样: 进入填写表单页面: 在表单页面填写信息,并提交: 表单数据验证 验证 ...
- Part 4:表单和类视图--Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...
随机推荐
- VMProtect修复导入表的插件
壳版本:VMProtect.Ultimate.2.12.3 样本:TKLobby.exe 目的:IAT修复 作者:MrWrong 标题:VMProtect修复导入表的插件 只是感兴趣,没有其他目的.失 ...
- HP Webinspect 10 访问wap的url
HP Webinspect是著名的扫描工具,这里讲一下怎么使用它扫wap的url. 通俗的讲,Wap是手机网页浏览器使用的网页,web是电脑网页浏览器使用的网页.(讲得不专业,但方便理解) 在手机上显 ...
- Android开发 解决AlertDialog中的EditText无法调出输入法的问题
在AlertDialog中使用自定义的View,如果View中有EditText,在上面点击,默认是跳不出软键盘的,不是焦点的问题.解决方法,有两种,一是把AlertDialog换成Dialog,但这 ...
- 【HDOJ】2722 Here We Go(relians) Again
根据矩阵建图,然后求最短路径. #include <cstdio> #include <cstring> #include <cstdlib> #define L ...
- Linux系统编程(26)——守护进程
Linux系统启动时会启动很多系统服务进程,比如inetd,这些系统服务进程没有控制终端,不能直接和用户交互.其它进程都是在用户登录或运行程序时创建,在运行结束或用户注销时终止,但系统服务进程不受用户 ...
- VC++6.0注释快捷键设置
在Qt Creator,eclipse等编辑器中,都默认有注释代码的快捷键:Ctrl + /. 注释快捷键在程序编程当中的作用相当明显,提高了编程效率.我在网上找到了一个在VC++6.0工具中添加注释 ...
- cocos2d-x 头文件中添加方法变量导致编译报错
代码如下: HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__ #include " ...
- Android调用系统相机和文件浏览器
//拍照功能,调用系统的相机功能 Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResul ...
- RequireJS进阶(三)
进阶的前面两篇讲述了r.js如何通过命令行把所有的模块压缩为一个js文件或把所有的css压缩为一个css文件.其中包括一些压缩配置参数的使用. 但以上两种方式有几个问题 1.通过命令手动配置压缩选项显 ...
- Ajax请求用户控件(.ascx)404错误