[英]Flask app won't load in browser
我刚开始使用flask,并且尝试使用Redis构建教程微博客。 这是我的应用程序:
from flask import Flask, render_template, request, url_for, redirect
import redis
from datetime import datetime
app = Flask(__name__)
app.config.from_object(__name__)
app.config.update(dict(
DEBUG = True,
))
POOL = redis.ConnectionPool(host='localhost', port=6379, db=0)
@app.route("/")
def index():
r = redis.StrictRedis(connection_pool = POOL)
print r
pipe = r.pipeline()
print pipe
last_ten = pipe.zrange('post:created_on', 0, 9)
print last_ten
posts = []
for post in last_ten:
posts.append(pipe.hgetall('{}{}'.format('post:', post)))
print posts
pipe.execute()
return render_template('index.html', posts)
@app.route('/new', methods = ['POST'])
def addPost():
r = redis.StrictRedis(connection_pool = POOL)
title = request.form['title']
author = request.form['author']
now = datetime.now()
pipe = r.pipeline()
post_format = 'post:'
pid = pipe.incr('post')
pipe.hmset('{}{}'.format(post_format,pid), {'title':title, 'author':author})
pipe.zadd('post:created_on', pid = now)
pipe.execute()
return redirect(url_for('index'))
if __name__ == "__main__":
app.run()
当我运行python testapp.py
我得到
* Running on http://127.0.0.1:5000/
* Restarting with reloader
但是,该页面永远不会从http://127.0.0.1:5000/
加载,也不会返回错误。 它只是挂起,试图永远加载。 我离开了一段时间,它一直在继续。 我不确定是什么原因导致的,但是谢谢您的帮助。
更新:我在index
视图中添加了一些print
语句,以查看代码运行时正在发生的事情,这就是在终端上打印的内容。
* Running on http://127.0.0.1:5000/
* Restarting with reloader
StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
我相信我已经解决了这个问题。 问题是我打开pipeline
并尝试对从db检索的数据进行处理,然后再调用pipe.execute
。 我仍在努力使整体功能正常工作,但这就是我解决此特定问题的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.