为什么wtf.quickform

推荐到广播
1171636 人聚集在这个小组
(铁桶僵尸biubiu)
(腿毛仙子)
(北葵赤尾)
(洛克华菲)
第三方登录:flask - Remove html5 attributes from input tags of Dom with WTF.quick_form() - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
I'm using the following command to generate forms with WTForms:
{{ wtf.quick_form(form)}}
I have a number of required fields, however I do not want to use the HTML5 required tag attributes because it results in an ugly style and user experience.
How can I remove the required attributes from my tags on the DOM?
7,2872379157
Maybe you can use JQuery ?
&script src="/ajax/libs/jquery/2.1.3/jquery.min.js"&&/script&
$(function(){
$("form").each(function(){
$(this).find(':input').each(function(){
this.removeAttr('required');
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25906
Stack Overflow works best with JavaScript enabled1402人阅读
Flask Web开发(10)
1、request.form获取POST请求中提交的表单数据
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/signin', methods=['GET'])
def signin_form():
return render_template('form.html')
@app.route('/signin', methods=['POST'])
def signin():
#receive the data from submit
username = request.form['username']
password = request.form['password']
if username=='admin' and password=='password':
return render_template('signin-ok.html', username=username)
return render_template('form.html', message='Bad username or password', username=username)
if __name__ == '__main__':
app.run()form.html:
&title&Please Sign In&/title&
{% if message %}
&p style=&color:red&&{{ message }}&/p&
{% endif %}
&form action=&/signin& method=&post&&
&legend&Please sign in:&/legend&
&p&&input name=&username& placeholder=&Username& value=&{{ username }}&&&/p&
&p&&input name=&password& placeholder=&Password& type=&password& value=&{{ password }}&&&/p&
&p&&button type=&submit&&Sign In&/button&&/p&
2、Flask-WTF扩展库处理Web表单:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from flask import Flask, render_template
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
#表单类,定义了一个文本字段和一个提交按钮
#Required()为验证函数
class NameForm(Form):
name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit')
#视图函数处理表单
@app.route('/', methods=['GET', 'POST'])
def index():
name = None
form = NameForm()
#如果提交的数据验证通过,则返回True
if form.validate_on_submit():
name = form.name.data
form.name.data = ''
return render_template('index.html', form=form, name=name)
if __name__ == '__main__':
manager.run()
index.html:
{% extends &base.html& %}
{% import &bootstrap/wtf.html& as wtf %}
{% block title %}Flasky{% endblock %}
{% block page_content %}
&div class=&page-header&&
&h1&Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!&/h1&
&!-- 导入wtf.html渲染整个Flsk-WTF表单,参数form是hello.py中传过来的form实例--&
{{ wtf.quick_form(form) }}
{% endblock %}
3、重定向和用户会话
Submit后,刷新网页会出现警告提示:
修改hello.py:(增加会话和重定向功能)
from flask import Flask, render_template, session, redirect, url_for
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
class NameForm(Form):
    name = StringField('What is your name?', validators=[Required()])
    submit = SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        session['name'] = form.name.data
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'))
if __name__ == '__main__':
    manager.run()
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9705次
排名:千里之外
原创:43篇
(21)(17)(6)flask 一直显示500 Internal Server Error
flask 一直显示500 Internal Server Error
在跟着《Flask Web开发:基于Python的Web应用开发实战》这本书中的例子写程序。
有两个页面,一个是主页面index,一个是user页面
打开主页会出现Internal Server Error,/user/xxx页面显示正常
from flask import Flask, render_template
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hack me'
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
class NameForm(Form):
name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit')
@app.errorhandler(404)
def page_note_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
@app.route('/', methods=['GET', 'POST'])
def index():
name = None
form = NameForm()
if form.validate_on_submit():
name = form.name.data
form.name.data = ''
return render_template('index.html', form=form, name=name, current_time=datetime.utcnow())
@app.route('/user/&name&')
def user(name):
return render_template('user.html', name=name)
if __name__ == '__main__':
manager.run()
index.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky{% endblock %}
{% block page_content %}
&div class="page-header"&
&h1&Hello ,{% if name %}{{ name }}{% else %}Stranger{% endif %}!&/h1&
{{ wtf.quick_form(form) }}
&p&the local time is {{ moment(current_time).format('LLL') }}&/p&
&p&that was {{ moment(current_time).fromnow(refresh=True) }}&/p&
{% endblock %}
{% extends "bootstrap/base.html" %}
{% block title %}Flasky{% endblock %}
&!--引用moment.js--&
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}
{% block head %}
{{ super() }}
&link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon"&
&link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon"&
{% endblock %}
{% block navbar %}
&div class="navbar navbar-inverse" role="navigation"&
&div class="container"&
&div class="navbar-header"&
&button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&
&span class="sr-only"&Toggle navigation&/span&
&span class="icon-bar"&&/span&
&span class="icon-bar"&&/span&
&span class="icon-bar"&&/span&
&a class="navbar-brand" href="/"&Flasky&/a&
&div class="navbar-collapse collapse"&
&ul class="nav navbar-nav"&
&li&&a href="/"&Home&/a&&/li&
{% endblock %}
{% block content %}
&div class="container"&
{% block page_content %}{% endblock %}
{% endblock %}
吐槽一下的编辑器:在编辑模式下排版正常,可是实际显示的样式和编辑模式下的排版不一样。
500内部错误,程序某个地方有问题
pythonapp.run(debug=True)
app.debug = True
打开调试模式,就可以在终端看到错误信息了。
http://dormousehole.readthedocs.org/en/latest/quickstart.html#debug-mode
Copyright & 2016 phpStudy

我要回帖

更多关于 quick time 的文章

 

随机推荐