python3.4 + Django1.7.7 表单的一些问题
上面是没有调用cleaned_data的提交结果,可见模版直接把form里面的整个标签都接收过来了
下面是调用cleaned_data 的结果
django 的表单,提交上来之后是这样的:
#coding: gb2312
from django import formsclass ContactForm(forms.Form):subject = forms.CharField(max_length=10,label='subject')#设置最大长度为10email = forms.EmailField(required=False,label='Email')#非必要字段message = forms.CharField(widget=forms.Textarea,label='message')#指定form中组件的类型#自定义校验规则,该方法在校验时被系统自动调用,次序在“字段约束”之后def clean_message(self):message = self.cleaned_data['message']#能到此处说明数据符合“字段约束”要求num_words = len(message.split())if num_words < 1:#单词个数raise forms.ValidationError("your word is too short!")return message
比如下面这句:
email = forms.EmailField(required=False,label='Email')#非必要字段
其实可以作为非必要字段,required=False
由于调用form.cleaned_data#只有各个字段都符合要求时才有对应的cleaned_data,之前好像必须得:
if form.is_valid():#说明各个字段的输入值都符合要求
所以上述字段required=False,在测试东西或者自己写东西,等安全性不高的场合就比较必要了
#coding: gb2312
from django.http import HttpResponse
import datetime,calendar
import time
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.shortcuts import redirect#from django import formfrom django.shortcuts import render
from .forms import ContactForm
#from django.shortcuts import render_to_response
#from django_manage_app.forms import ContactFormdef current_datetime(request):now = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))html = '<html><body>It is now %s.</body></html>' %nowreturn HttpResponse(html)def show_readme(request):if request.method == 'POST':#提交请求时才会访问这一段,首次访问页面时不会执行form = ContactForm(request.POST)print (form['subject'])print (form['email'])print (form['message'])print ("show ----------------")#“首次访问”和“提交的信息不符合要求”时被调用return render_to_response('show.html', {'form': form})def contact_author(request):if request.method == 'POST':#提交请求时才会访问这一段,首次访问页面时不会执行form = ContactForm(request.POST)if form.is_valid():#说明各个字段的输入值都符合要求cd = form.cleaned_data#只有各个字段都符合要求时才有对应的cleaned_data#print (form.cleaned_data())print (cd['subject'])print (cd['email'])print (cd['message'])#return render_to_response('contact_author.html', {'form': form})#return redirect(reverse('','show_readme.html'))#return HttpResponseRedirect('/thanks/') return render_to_response('show_readme.html', {'form': cd})#此处逻辑应该是先生成新的预览页面,再保存为txt#return responseelse:#首次访问该url时没有post任何表单form = ContactForm()#第一次生成的form里面内容的格式print (form)print (form.is_valid())#“首次访问”和“提交的信息不符合要求”时被调用return render_to_response('contact_author.html', {'form': form})#return render_to_response('show.html', {'form': form})def thanks(request):return render_to_response('thanks.html')def download_file(request): #from django.http import HttpResponse ## CSV #import csv #response = HttpResponse(mimetype='text/csv') #response['Content-Disposition'] = 'attachment; filename=my.csv' #writer = csv.writer(response) #writer.writerow(['First row', 'Foo', 'Bar', 'Baz']) #writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"]) # Text file response = HttpResponse(content_type='text/plain') response['Content-Disposition'] = 'attachment; filename=my.txt' response.write("aa\n") response.write("bb") # PDF file #http://code.djangoproject.com/svn/django/branches/0.95-bugfixes/docs/outputting_pdf.txt #from reportlab.pdfgen import canvas #need pip ind#response = HttpResponse()#)mimetype='application/pdf') #response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' #p = canvas.Canvas(response) #p.drawString(100, 100, "Hello world.") #p.showPage() #p.save() #response = HttpResponse()fout=open("mysite//test.txt","wt") str = "hello world"fout.write(str)fout.close() #response['Content-Disposition'] = 'attachment; filename=test.txt' data = open("mysite//test.txt", "rb").read()html = '<html><body>%s</body></html>' %strreturn response#HttpResponse(data, content_type="text/plain")
提交给模版的html:
<html>
<style type="text/css">.field{background-color:#BCD8F5;}
</style>
<head><title>show readme</title>
</head>
<body><!<div class="field">{{ form.subject }}{{ form.email }}{{ form.message }}<!</div></body>
</html>
Django本身内建有一些app,例如注释系统和自动管理界面。
app的一个关键点是它们是很容易移植到其他project和被多个project复用。
对于如何架构Django代码并没有快速成套的规则。
如果你只是建造一个简单的Web站点,那么可能你只需要一个app就可以了;
但如果是一个包含许多不相关的模块的复杂的网站,
例如电子商务和社区之类的站点,那么你可能需要把这些模块划分成不同的app,以便以后复用。
数据库模型有有效性验证
C:\Python27\Lib\site-packages\Django-1.7.1-py2.7.egg\django\bin\mysite>python manage.py sqlall books
CommandError: App 'books' has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
此时需要输入如下部分即可
C:\Python27\Lib\site-packages\Django-1.7.1-py2.7.egg\django\bin\mysite>python manage.py makemigrations
C:\Python27\Lib\site-packages\Django-1.7.1-py2.7.egg\django\bin\mysite>python manage.py migrate
若上述问题依旧:
Since there is still a bit of backwards compatibility with django 1.6 and below you can still use the sql commands from django-admin. However, you have to delete the migrations folder first.
To get the create statements you need to remove the migrations folder
直接删除books app下面的migrations文件夹
转载于:https://www.cnblogs.com/wangyaning/p/7854007.html
python3.4 + Django1.7.7 表单的一些问题相关推荐
- python表单填写_Python3.4 splinter(模拟填写表单)使用方法
如下所示: from splinter.browser import Browser b = Browser('chrome') url = 'https://kyfw.12306.cn/otn/le ...
- 【Python3爬虫】当爬虫碰到表单提交,有点意思
一.写在前面 我写爬虫已经写了一段时间了,对于那些使用GET请求或者POST请求的网页,爬取的时候都还算得心应手.不过最近遇到了一个有趣的网站,虽然爬取的难度不大,不过因为表单提交的存在,所以一开始还 ...
- Python3.5 Django1.10 Scrapy1.2 Ubuntu16.04 HTML5
1.Python3.5 1. 虚拟环境 venv python3.4 创建虚拟环境(py3.4自带venv,不需要安装) Ubuntu 16.4 python3.5升级python3.6 sudo a ...
- python表单切换_python3 selenium自动化 frame表单嵌套的切换方法
在web自动化测试中,测试工程师经常会碰到frame表单嵌套结构,直接定位会报错,我们需要切换表单后才能成功定位. 我拿QQ邮箱登录来作为例子说下frame怎么切换. qq邮箱页面按F12可以看到以下 ...
- django,form表单,数据库增删改查
Django 02 复习: Django下载: 在命令行输入: pip3 install django==1.11.11 在这里不要用最新的3.7,推荐使用3.6/3.5/ ...
- python图书馆抢座_python模拟表单提交登录图书馆
本文实例为大家分享了python模拟登录图书馆的具体代码,供大家参考,具体内容如下 模拟表单提交的原理: 我们都知道Http是无状态的,所以当我们提交的数据和浏览器中正常提交一样,那么服务器就会返回和 ...
- python自动登录并提交表单_用python模拟登录(解析cookie + 解析html + 表单提交 + 验证码识别 + excel读写 + 发送邮件)...
老婆大人每个月都要上一个网站上去查数据,然后做报表. 为了减轻老婆大人的工作压力,所以我决定做个小程序,减轻我老婆的工作量. 准备工作 1.tesseract-ocr 这个工具用来识别验证码,非常好用 ...
- python怎么自动化录入数据_Python自动化办公之–读取Excel 录入表单
前言 本篇内容,让你完全掌握Python是如何自动化办公的~ 一.环境准备 1.1 Python 3.7.0 1.2 Pycharm (Python 开发工具) 1.3 Selenium ...
- html表单如何空格,如何使用Flas在HTML表单中带空格的字符串
我尝试使用Flask和python3.6构建一个简单的在线测试,使用带有单选按钮的HTML表单在Flask路径之间传送选定的答案.第一步是选择测验的类别,然后进入实际测验页面,如下所示:app = F ...
最新文章
- python时间处理模块datetime+dateutil、numpy时间处理模块datetime64以及pandas时间处理模块Timestamp的演化路径及常用处理接口
- 原创 | 变分自动编码器(VAE)
- imx6 uboot lvds clock
- 监督学习与无监督学习的区别_机器学习
- 2021年了,`IEnumerator`、`IEnumerable`接口还傻傻分不清楚?
- [数据结构-严蔚敏版]P48栈的链式表示
- 转型产品经理该怎么做(适用于0-2岁的产品经理)
- cocos2d c 调用java_cocos2d-x之C++ 调用Java函数并接收返回值
- 触发父组件变量_Vue组件之间的传值
- Zabbix-server安装后的故障处理及Zabbix-agent的配置添加过程
- 推荐几个非常有用的工具
- XAML实例教程系列 - 标记扩展(Markup Extensions)
- VS MFC checkbox 控件默认选中
- 为程序员准备的英语学习资料
- 第五篇 python计算新版个人所得税
- Linux 配置SSH免密登录
- python中pandas什么意思_python pandas 基础理解
- 光圈,快门, 曝光,焦距, ISO,景深。
- 调用企业微信API,ios端异常
- bidirectional PIM
热门文章
- linux 内核互斥体,内核并发控制---互斥量
- java redis pubsub_如何从Java中的生菜RedisPubSubListener获取消息?
- mysql 只有 .ibd_mysql数据库被破坏,只剩下ibd文件时如何恢复
- matlab图片包微盘,如何用Matlab绘制二维图形资料下载
- php类微博源码,类微博功能设计
- Android自定义ScrollBar,android自定义View之垂直的滚动条
- R语言自动化报告格式——knitr
- LSTM网络(Long Short-Term Memory )
- Java基础复习---线程创建
- Linux运维 第三阶段 (一) 网络配置及openssl加密