标签:
CSRF_ENABLED=True SECRET_KEY='you-will-never-guess'
from flask import Flask app=Flask(__name__) app.config.from_object('config') from app import views #读取配置文件
from flask.ext.wtf import Form from wtforms import StringField,BooleanField from wtforms.validators import DataRequired class LoginForm(Form): openid = StringField('openid',validators=[DataRequired()]) remember_me = BooleanField('remember_me',default=False)
{% extends "base.html" %} {% block content %} <h1>Sigh In</h1> <form method="post" action="" name="login"> {{form.hidden_tag()}} <p> Please enter your OpenID:<br> {{form.openid(size=80)}}<br> </p> <p>{{form.remember_me}} Remember Me</p> <p><input type="submit" value="Sign In"></p> </form> {% endblock %}
from flask import render_template from app import app from .forms import LoginForm @app.route('/') @app.route('/index') def index (): user={'nickname':'Bob'} posts=[ {'author':{'nickname':'John'}, 'body':'Beautiful day in Portland!'}, {'author':{'nickname':'Susan'}, 'body':'The Avengers movie was so cool!'} ] return render_template("index.html", title="Home", user=user, posts=posts) @app.route('/login',methods=['GET','POST']) def login (): form = LoginForm() return render_template("login.html", title = "Sign In", form = form) #导入LoginForm,视图接受GET和POST请求 #实例化一个LoginForm
from flask import render_template,flash,redirect from app import app from .forms import LoginForm @app.route('/') @app.route('/index') def index (): user={'nickname':'Bob'} posts=[ {'author':{'nickname':'John'}, 'body':'Beautiful day in Portland!'}, {'author':{'nickname':'Susan'}, 'body':'The Avengers movie was so cool!'} ] return render_template("index.html", title="Home", user=user, posts=posts) @app.route('/login',methods=['GET','POST']) def login (): form = LoginForm() if form.validate_on_submit(): flash('login requested for OpenID="'+form.openid.data+'",remember_me='+str(form.remember_me.data)) return redirect('/index') return render_template("login.html", title = "Sign In", form = form) #form.validate_on_submit()判断是否提交 #提交后用flash( )传递数据 #redirect( )是url跳转
<html> <head> {% if title %} <title>{{title}} - myblog</title> {% else %} <title>Welcome - myblog</title> {% endif %} </head> <body> <div>MyBlog:<a href="/index">Home</a></div> <hr> {% with messages = get_flashed_messages() %} {% if messages %} <ul> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} {% block content %} {% endblock%} </body> </html>
{% extends "base.html" %} {% block content %} <h1>Sigh In</h1> <form method="post" action="" name="login"> {{form.hidden_tag()}} <p> Please enter your OpenID:<br> {{form.openid(size=80)}}<br> {% for error in form.openid.errors %} <span style="color:red;">[{{ error }}]</span> {% endfor %}<br> </p> <p>{{form.remember_me}} Remember Me</p> <p><input type="submit" value="Sign In"></p> </form> {% endblock %}
标签:
原文地址:http://blog.csdn.net/u013055678/article/details/51365779