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 %}
    
  • 相关阅读:
    js 每个月有多少天算法
    js 树的操作
    画线
    程序员如何防止脑疲劳
    汉字求出拼音缩写
    datagird 多行外于编辑状态
    overflow: hidden 失效
    CSS 相对/绝对(relative/absolute)定位系列(三)
    display:inlineblock在Chrome与FF下导致的间隙
    css ul li 的使用及浏览器兼容问题
  • 原文地址:https://www.cnblogs.com/keer2345/p/6045732.html
Copyright © 2011-2022 走看看