FormView用法
功能描述:
学生可以对相应学校机构进行投诉建议.
form表单
- class SuggestForm(forms.Form):
- TYPE_CHOICES = (
- (0, u'学校'),
- (1, u'学院'),
- (2, u'校院'),
- )
- content = forms.CharField(label=u"内容", required=True, widget=forms.Textarea(attrs={'cols': 33}),
- error_messages={'required': u'请留下你的建议哦'})
- type = forms.ChoiceField(label=u"类型", required=True, widget=forms.RadioSelect, choices=TYPE_CHOICES,
- error_messages={'required': u'请做出你的选择哦'})
- error_messages = {
- 'content': {
- 'required': "内容不能为空哦"
- },
- }
- def save(self, user, commit=True):
- cleaned_data = super(SuggestForm, self).clean()
- student = Student.objects.get(user=user)
- complain = Complain(student=student, content=cleaned_data.get("content"))
- complain.save()
前台显示
- <form action="" method="post">
- {% csrf_token %}
- {{ form.as_table }}
- <input type="submit" value="提交" class="ahref"/>
- </form>
view定义
- from django.views.generic import FormView
class SuggestView(FormView):- template_name = "student/suggest.html"
- form_class = SuggestForm
- def form_valid(self, form):
- form.save(self.request.user)
- return render(self.request, self.template_name, {"success": u"提交建议成功"})
- #===如果需要变更样式, 或者变更显示中文名称.更改 label
- def __init__(self, *args, **kwargs):
- super(TestModelForm, self).__init__(*args, **kwargs)
- #====改变样式,也可以赋值 class=???,在外面html页面上先定义好,个人不推荐直接在代码里写,只是为了演示。
- self.fields['yourname'].widget.attrs.update({'style' : 'border:1px dashed #ccc;'})
- self.fields['email'].label='伊妹儿'
FormView用法的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
随机推荐
- HelloX项目github协同开发指南
概述 为了提高协同开发效率,HelloX项目已托管到github网站上.根据目前的开发进展,创建了下列几个子项目: HelloX操作系统内核项目:https://github.com/hellox-p ...
- iOS开发中,应用内直接跳转到Appstore
iOS开发中,应用内直接跳转到Appstore 1.进入appstore中指定的应用NSString *str = [NSString stringWithFormat: ...
- golang printf
1: 打印包括字段在内的实例的完整信息 同 %+V fmt.Printf("Hello world! %v","hufeng") 输出:Hello world ...
- MVC5.0 中如何提高Controller 的优先级
//在area下建立的Home namespace WebApplication8.Areas.Weather.Controllers { public class HomeController : ...
- SQL Server索引进阶:第七级,过滤的索引
原文地址: Stairway to SQL Server Indexes: Level 7,Filtered Indexes 本文是SQL Server索引进阶系列(Stairway to SQL S ...
- spring 加载配置文件的相关配置总结
PropertyPlaceholderConfigurer 注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:p ...
- HDU2008-数值统计
描述: 统计给定的n个实数中,负数.零和正数的个数. 代码: #include<stdio.h> #include<string.h> #include<iostream ...
- 5.4.3 RegExp构造函数属性
RegExp构造函数包含一些属性(这些属性在其他语言中被看成是静态属性).这些属性适用于作用域中的所有正则表达式,并且基于所执行的最近一次正则表达式操作而变化.关于这些属性的另一个独特之处,就是可以通 ...
- 加密PHP文件的方式,目测这样可以写个DLL来加密了
<?php function encode_file_contents($filename) { $type=strtolower(substr(strrchr($filename,'.'),1 ...
- win7 资源管理器的背景色修改
主要参考 http://blog.sina.com.cn/s/blog_49c182c20100w3nb.html win7 通过dll修改背景色首先找到这个文件C:\Windows\Resource ...