zoukankan      html  css  js  c++  java
  • flask web development Chapter04

    Web Forms

    Cross-Site Request Forgery (CSRF) Protection

    hello.py: Flask-WTF configuration

    app.config['SECRET_KEY'] = 'hard to guess string'
    

    Form Classes

    hello.py: Form class definition

    from flask.ext.wtf import Form
    from wtforms import StringField, SubmitField
    from wtforms.validators import Required
    
    class NameForm(Form):
      name = StringField('What is your name?', validators=[Required()])
      submit = SubmitField('Submit')
    

    hello.py: Route methods

    @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)
    

    templates/index.html: Using Flask-WTF and Flask-Bootstrap to render a form

    {% 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>
    </div>
    {{ wtf.quick_form(form) }}
    {% endblock %}
    

    Redirects and User Sessions

    hello.py: Redirects and user sessions

    from flask import Flask, render_template, session, redirect, url_for
    @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'))
    

    Message Flashing

    hello.py: Flashed messages

    from flask import Flask, render_template, session, redirect, url_for, flash
    @app.route('/', methods=['GET', 'POST'])
    def index():
      form = NameForm()
      if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
          flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        form.name.data = ''
        return redirect(url_for('index'))
      return render_template('index.html',form = form, name = session.get('name'))
    

    templates/base.html: Flash message rendering

    {% block content %}
    <div class="container">
      {% for message in get_flashed_messages() %}
      <div class="alert alert-warning">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        {{ message }}
      </div>
      {% endfor %}
      {% block page_content %}{% endblock %}
    </div>
    {% endblock %}
    
  • 相关阅读:
    I.MX6 mkuserimg.sh 使用
    【HighCharts系列教程】五、版权属性——Credits
    【HighCharts系列教程】四、颜色属性——colors
    【HighCharts系列教程】三、图表属性——chart
    【HighCharts系列教程】二、Highcharts结构及API文档
    【HighCharts系列教程】一、认识Highcharts
    higncharts 编辑Highcharts.com链接
    higncharts 去掉Highcharts.com链接
    Highcharts一些属性
    如何用easyui+JAVA 实现动态拼凑datagrid表格
  • 原文地址:https://www.cnblogs.com/keer2345/p/6045732.html
Copyright © 2011-2022 走看看