flask 有人研究吗?谁能帮我看看这段代码?

Evenvi 发布于 2012/10/18 22:05
阅读 2K+
收藏 0
#all the imports
from __future__ import with_statement
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from contextlib import closing


#configuration
DATABASE='/tmp/flaskr.db'
DEBUG=True
SECRET_KEY='development key'
USERNAME='admin'
PASSWORD='default'


#create our little application^_^
app = Flask(__name__)
app.config.from_object(__name__)


#load config 'file'
app.config.from_envvar('FLASKR_SETINGS', silent=True)


#connect db
def connect_db():
return sqlite3.connect(app.config['DATABASE'])


def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read())
db.commit()


@app.before_request
def before_request():
g.db=connect_db()


@app.after_request()
def after_request(response):
g.db.close()
return response


#show entries
@app.route('/')
def show_entries():
cur=g.db.execute('select title, text from entries order by id desc')
entries = [dict(title=row[0], text=row[1]) for raw in cur.fetchall()]
return render_template('show_entries.html', entries=entries)


#add entries
@app.route('/add', methods=['POST'])
def add_entries():
if not session.get('logged_in'):
abort(401)
g.db.execute('insert into entries (title, text) values(?,?)',[request.form('title'),request.form('text')])
g.db.commit()
flash('New entry was successfuly post')
return redirect(url_for('show_entries'))


#user login
@app.route('/login',methods=['GET','POST'])
def login():
error=None
if methods.post=='POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['passowrd']!=app.config['PASSWORD']:
error='Invalid password'
else:
session['logged_in']=True
flash('You were logged_in')
return redirect(url_for('show_entries'))
return render_template('login.html',error=error)


#user logout
@app.route('/logout')
def logout():
session.pop('logged_in',None)
flash('You were logout ')
return redirect(url_for('show_entries'))


if __name__ == '__main__':

app.run()

学习的flask博客程序,after_request()函数貌似有问题,求解答

加载中
0
messense
messense
@app.after_request() ===〉 @app.after_request
Evenvi
Evenvi
谢啦,好低级的错误
0
不必在乎朕是谁
不必在乎朕是谁
你有没有在 BAE 上试着部署呢?我在部署的时候出现了问题……
0
yoory29
yoory29

#configuration DATABASE='/tmp/flaskr.db'
DEBUG=True
SECRET_KEY='development key'
USERNAME='admin'
PASSWORD='default'


我也初学

这里是不是用

app.config['xxxx'] 这种方式更好?

返回顶部
顶部