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 %}
    
  • 相关阅读:
    利用Filter和拦截器,将用户信息动态传入Request方法
    Spring统一返回Json工具类,带分页信息
    ASP.NET MVC4 学习记录
    lightgbm 的简单实践案例
    numpy和pandas 各种比较常见的用法总结
    【数据预处理】如何将某一类别特征的极少数类别归为一类
    十大经典算法之Apriori 算法
    常用函数框架
    【解决方案】django初始化执行python manage.py migrate命令后,除default数据库之外的其他数据库中的表没有创建出来
    【解决方案】M2Crypto不支持python3
  • 原文地址:https://www.cnblogs.com/keer2345/p/6045732.html
Copyright © 2011-2022 走看看