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 定义函数的参数的个数,传入参数的个数对调用的影响
    jQuery常见操作总结
    jQuery hover事件
    Ajax实现登陆并友好提示错误信息
    JS实现弹出登录框
    .NET源码 SortedSet(红黑树)
    C#链接SQL Server数据库
    628. Maximum Product of Three Numbers 最大的三个数的乘积
    501. Find Mode in Binary Search Tree 找二叉搜索树的众数
    235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA
  • 原文地址:https://www.cnblogs.com/keer2345/p/6045732.html
Copyright © 2011-2022 走看看