django.views.generic中的CreateView类,是基于View的子类。CreateView可以简单快速的创建表对象。

下面记录小作代码。

# polls/views.py

from django.views.generic import CreateView

class QuestionCreate(CreateView):
form_class = QuestionForm # 表类
template_name = 'polls/question_form.html' # 添加表对象的模板页面
success_url = '/polls/thanks' # 成功添加表对象后 跳转到的页面 def form_invalid(self, form): # 定义表对象没有添加失败后跳转到的页面。
return HttpResponse("form is invalid.. this is just an HttpResponse object")
# polls/forms.py 

from django.forms import ModelForm
from polls.models import * class QuestionForm(ModelForm):
class Meta:
model = Question
fields = '__all__'
# polls/models.py

from django.db import models

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') def __unicode__(self):
return self.question_text + '\n' + str(self.pub_date)
# polls/question_form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<form method="post">
{% csrf_token %}
{{ form.as_p }} # {{ form.as_p }}会按QuestionForm中 Meta的fields的列表,列出需要添加的字段。
<input type="submit" value="Submit" />
</form> </body>
</html>
# polls/thanks.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thanks</title>
</head>
<body>
Thanks!
</body>
</html>
# polls/urls.py

from django.conf.urls import url
from . import views urlpatterns = [ url(r'^questioncreate/$', views.QuestionCreate.as_view(), name='QuestionCreate'), ]

QuestionCreate继承了CreateView, form_class指定了表单QuestionForm, QuestionForm指定了数据表Question, template_name指定了添加表对象的模板, success_url指定了添加表对象成功后跳转的页面, 
												

Django CreateView 简单使用的更多相关文章

  1. Django - Django框架 简单介绍

    Django框架 简单介绍 本文地址: http://blog.csdn.net/caroline_wendy/article/details/29172271 1. 介绍 Django是一个开放源码 ...

  2. Python django实现简单的邮件系统发送邮件功能

    Python django实现简单的邮件系统发送邮件功能 本文实例讲述了Python django实现简单的邮件系统发送邮件功能. django邮件系统 Django发送邮件官方中文文档 总结如下: ...

  3. django的简单原理

    一.自定义客户端和服务端的请求响应 1.客户端打开url,向服务器发出请求 2.服务端用socket写一个py,用于接收请求和做出响应 3.服务端接收请求 4.服务端模拟HTTP协议做出响应,状态行为 ...

  4. Django中简单添加HTML、css、js等文件(非正规添加,适合小白)

    Django中简单添加HTML.css.js等文件 首先申明下自己的环境, python版本3.65(亲测3.7版本有毒,没解决掉!) Django版本1.11.15(版本比较成熟,也可以用最新的版本 ...

  5. 沉淀,再出发:Django的简单使用

    沉淀,再出发:Django的简单使用 一.前言     在学习了python的基础语法之后,其实大家都很怀疑python的使用场景,其实python在很多场合都有很强的适应性,就比如说web开发之中使 ...

  6. 基于Django进行简单的微信开发

    代码地址如下:http://www.demodashi.com/demo/11756.html 一.微信公众号的准备: 1. 注册 访问地址:https://mp.weixin.qq.com/ 按照提 ...

  7. Python Django 实现简单注册功能

    Python Django 实现简单注册功能 项目创建略,可参考前期文档介绍. 目录结构如下 编辑views.py from django.shortcuts import render # Crea ...

  8. Django开发简单采集用户浏览器信息的小功能

    Django开发简单采集用户浏览器信息的小功能 Centos环境准备 yum install –y python-pip export http_proxy=http://10.11.0.148:80 ...

  9. django入门 -- 简单流程

    django入门 -- 简单流程 简介 通过简单示例,使用django完成基本流程的开发,学习django的主要的知识点,在后续课程中会逐个知识点进行深入讲解 以“图书-英雄”管理为示例 主要知识点介 ...

随机推荐

  1. File.Exists(Application.StartupPath + \\Settings\\Settings.xml)

    File.Exists(Application.StartupPath + "\\Settings\\Settings.xml")

  2. [转]Add Bootstrap Glyphicon to Input Box

    本文转自:http://stackoverflow.com/questions/18838964/add-bootstrap-glyphicon-to-input-box How can I add ...

  3. Spring cloud ReadTimeout 问题解决

    今天使用Spring cloud @FeignClient 调用远程服务的时候,出现readTimeout问题,通过找资料解决方式如下 在Spring.properties 配置文件中添加如下属性解决 ...

  4. 多个tomcat配置

    在centos7.3下搭建jenkins自动部署环境,需要一个tomcat来启动jenkins,另一个用来自动部署的位置,因此需要两个tomcat同时运行,并且在自动构建后能够启动项目,又不会关闭je ...

  5. JBPM学习第6篇:通过Git导入项目

    1.登记到工作台 切换到目录: $SERVER_HOME/bin/ for Unix environment: ./standalone.shfor Windows environment: ./st ...

  6. jquery not() 方法

    1.not(expression) 根据表达式参数的值,从包装集里删除元素 example : $('img[alt]').not('[alt*=joy]') 返回包含属性alt的img元素,但img ...

  7. ubuntu终端颜色设置

    在 .bashrc中增加 PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\]\u @ \h\[\033[00m\]:\[\033[00;34m ...

  8. 空白符对HTML结构的影响与解决方案

    何为空白符? 空白符: 空格.制表符.换行符 注意:浏览器在解析HTML时会把所有空白符合并成一个空格 空白符对HTML结构的影响 HTML5中<textarea>标签placeholde ...

  9. 学习笔记flexbox新布局

    FlexBox简称“弹性盒子”,除了用于实现弹性布局,还可以用来居中内容,改变标记中的源码顺序.首先说明IE9及以下浏览器不支持FlexBox. .flex{ display:flex; flex:1 ...

  10. C++中long是什么类型

    long long本质上还是整型,只不过是一种超长的整型. int型:32位整型,取值范围为-2^31 ~ (2^31 - 1) .long:在32位系统是32位整型,取值范围为-2^31 ~ (2^ ...