当我尝试访问https://127.0.0.1:5000/deliveries_awaiting_pickup时,我收到内部服务器错误(500)。当我尝试访问它时,运行Flask服务器的终端看起来像这样(在多次尝试访问它之后):
$ python minitwitLocal.py
table user already exists
* Running on https://127.0.0.1:5000/ (Press CTRL+C to quit)
hello
[]
127.0.0.1 - - [13/Apr/2015 21:26:57] "GET /deliveries_awaiting_pickup HTTP/1.1" 500 -
hello
[]
127.0.0.1 - - [13/Apr/2015 21:26:59] "GET /deliveries_awaiting_pickup HTTP/1.1" 500 -
我尝试删除数据库并重新启动服务器,但它不起作用。此外,该表是空的,但我希望它呈现一个空白页。
如何让它渲染模板?谢谢!
minitwitLocal.py的最相关部分:
@app.route('/deliveries_awaiting_pickup')
def deliveries_awaiting_pickup():
if not g.user:
return redirect(url_for('public_timeline'))
print "hello"
orders=query_db('''select * from deliveries_awaiting_pickup''')
print orders
return render_template('deliveries_awaiting_pickup.html', orders)
deliveries_awaiting_pickup.html:
{% extends "layout.html" %}
{% block title %}
Deliveries Awaiting Pickup
{% endblock %}
{% block body %}
<h2>{{ self.title() }}</h2>
Deliveries Awaiting Pickup go here<br><br>
{% if orders %}
{% for order in orders %}
{% endfor %}
{% endif %}
{% endblock %}
minitwitLocal.py :(主要是Flask提供的“minitwit”示例,只需进行一些修改)
# -*- coding: utf-8 -*-
"""
MiniTwit
~~~~~~~~
A microblogging application written with Flask and sqlite3.
:copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import time
from sqlite3 import dbapi2 as sqlite3
from hashlib import md5
from datetime import datetime
from flask import Flask, request, session, url_for, redirect, \
render_template, abort, g, flash, _app_ctx_stack
from werkzeug import check_password_hash, generate_password_hash
import requests
import json
# configuration
DATABASE = 'minitwit.db'
PER_PAGE = 30
DEBUG = True
SECRET_KEY = 'development key'
#FOURSQUARE OAUTH2
FOURSQUARE_CLIENT_ID = 'XXXXXXX'
FOURSQUARE_CLIENT_SECRET = 'XXXXXX'
FOURSQUARE_REDIRECT_URI = "https://127.0.0.1:5000/authenticated"
FOURSQUARE_API_BASE = "https://api.foursquare.com/v2/"
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
top = _app_ctx_stack.top
if not hasattr(top, 'sqlite_db'):
top.sqlite_db = sqlite3.connect(app.config['DATABASE'])
top.sqlite_db.row_factory = sqlite3.Row
return top.sqlite_db
@app.teardown_appcontext
def close_database(exception):
"""Closes the database again at the end of the request."""
top = _app_ctx_stack.top
if hasattr(top, 'sqlite_db'):
top.sqlite_db.close()
def init_db():
"""Creates the database tables."""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
try:
db.cursor().executescript(f.read())
except sqlite3.OperationalError, msg:
print msg
db.commit()
def query_db(query, args=(), one=False):
"""Queries the database and returns a list of dictionaries."""
cur = get_db().execute(query, args)
rv = cur.fetchall()
return (rv[0] if rv else None) if one else rv
def get_user_id(username):
"""Convenience method to look up the id for a username."""
rv = query_db('select user_id from user where username = ?',
[username], one=True)
return rv[0] if rv else None
def format_datetime(timestamp):
"""Format a timestamp for display."""
return datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d @ %H:%M')
def gravatar_url(email, size=80):
"""Return the gravatar image for the given email address."""
return 'http://www.gravatar.com/avatar/%s?d=identicon&s=%d' % \
(md5(email.strip().lower().encode('utf-8')).hexdigest(), size)
@app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = query_db('select * from user where user_id = ?',
[session['user_id']], one=True)
@app.route('/add_new_delivery_order')
def add_new_delivery_order():
if not g.user:
return redirect(url_for('public_timeline'))
return render_template('add_new_delivery_order.html')
@app.route('/deliveries_awaiting_pickup')
def deliveries_awaiting_pickup():
if not g.user:
return redirect(url_for('public_timeline'))
print "hello"
orders=query_db('''select * from deliveries_awaiting_pickup''')
print orders
return render_template('deliveries_awaiting_pickup.html', orders)
@app.route('/')
def deliveries_in_progress():
if not g.user:
return redirect(url_for('public_timeline'))
return render_template('deliveries_in_progress.html')
@app.route('/completed_deliveries')
def completed_deliveries():
if not g.user:
return redirect(url_for('public_timeline'))
return render_template('completed_deliveries.html')
@app.route('/deliveries_waiting_for_bids')
def deliveries_waiting_for_bids():
if not g.user:
return redirect(url_for('public_timeline'))
return render_template('deliveries_waiting_for_bids.html')
@app.route('/authenticated')
def authenticated():
db = get_db()
CODE = request.args.get('code', '')
response = requests.get("https://foursquare.com/oauth2/access_token?client_id=" + FOURSQUARE_CLIENT_ID
+ "&client_secret=" + FOURSQUARE_CLIENT_SECRET + "&grant_type=authorization_code&redirect_uri="
+ FOURSQUARE_REDIRECT_URI +"&code=" + CODE + "&sort=newestfirst")
dict = json.loads(response.text)
db.execute('insert into access_token (user_id, access_token_text) values (?, ?)',
[session['user_id'], dict['access_token']])
db.commit()
return redirect(url_for('foursquare'), code=302)
@app.route('/foursquare')
def foursquare():
"""Shows your foursquare info. Or, if you have not authorized this app to connect to
foursquare, then it will redirect you to foursquare.
"""
if not g.user:
return redirect(url_for('public_timeline'))
result = query_db('select access_token_text from access_token where user_id = ?',
[session['user_id']], one=True)
if not result:
return redirect("https://foursquare.com/oauth2/authenticate?response_type=code&client_id=" + FOURSQUARE_CLIENT_ID + "&redirect_uri=" + FOURSQUARE_REDIRECT_URI,code=302)
else:
#Get info from foursquare
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
list = dict['response']['checkins']['items']
return render_template('foursquare.html', listOfCheckins=list)
@app.route('/')
def timeline():
"""Shows a users timeline or if no user is logged in it will
redirect to the public timeline. This timeline shows the user's
messages as well as all the messages of followed users.
"""
if not g.user:
return redirect(url_for('public_timeline'))
#Get info from foursquare
result = query_db('select access_token_text from access_token where user_id = ?',
[session['user_id']], one=True)
if result:
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
item = dict['response']['checkins']['items'][0]
return render_template('timeline.html',messages=query_db('''
select message.*, user.* from message, user
where message.author_id = user.user_id and (
user.user_id = ? or
user.user_id in (select whom_id from follower
where who_id = ?))
order by message.pub_date desc limit ?''',
[session['user_id'], session['user_id'], PER_PAGE]),checkin=item)
return render_template('timeline.html',messages=query_db('''
select message.*, user.* from message, user
where message.author_id = user.user_id and (
user.user_id = ? or
user.user_id in (select whom_id from follower
where who_id = ?))
order by message.pub_date desc limit ?''',
[session['user_id'], session['user_id'], PER_PAGE]),checkin=None)
@app.route('/public')
def public_timeline():
"""Displays the latest messages of all users."""
return render_template('timeline.html', messages=query_db('''
select message.*, user.* from message, user
where message.author_id = user.user_id
order by message.pub_date desc limit ?''', [PER_PAGE]))
@app.route('/<username>')
def user_timeline(username):
"""Display's a users tweets."""
profile_user = query_db('select * from user where username = ?',
[username], one=True)
if profile_user is None:
abort(404)
followed = False
if g.user:
followed = query_db('''select 1 from follower where
follower.who_id = ? and follower.whom_id = ?''',
[session['user_id'], profile_user['user_id']],
one=True) is not None
#Get info from foursquare
result = query_db('select access_token_text from access_token where user_id = ?',
[profile_user['user_id']], one=True)
if result:
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
item = dict['response']['checkins']['items'][0]
return render_template('timeline.html', messages=query_db('''
select message.*, user.* from message, user where
user.user_id = message.author_id and user.user_id = ?
order by message.pub_date desc limit ?''',
[profile_user['user_id'], PER_PAGE]), followed=followed,
profile_user=profile_user, checkin=item)
@app.route('/<username>/follow')
def follow_user(username):
"""Adds the current user as follower of the given user."""
if not g.user:
abort(401)
whom_id = get_user_id(username)
if whom_id is None:
abort(404)
db = get_db()
db.execute('insert into follower (who_id, whom_id) values (?, ?)',
[session['user_id'], whom_id])
db.commit()
flash('You are now following "%s"' % username)
return redirect(url_for('user_timeline', username=username))
@app.route('/<username>/unfollow')
def unfollow_user(username):
"""Removes the current user as follower of the given user."""
if not g.user:
abort(401)
whom_id = get_user_id(username)
if whom_id is None:
abort(404)
db = get_db()
db.execute('delete from follower where who_id=? and whom_id=?',
[session['user_id'], whom_id])
db.commit()
flash('You are no longer following "%s"' % username)
return redirect(url_for('user_timeline', username=username))
@app.route('/add_message', methods=['POST'])
def add_message():
"""Registers a new message for the user."""
if 'user_id' not in session:
abort(401)
if request.form['text']:
db = get_db()
db.execute('''insert into message (author_id, text, pub_date)
values (?, ?, ?)''', (session['user_id'], request.form['text'],
int(time.time())))
db.commit()
flash('Your message was recorded')
return redirect(url_for('timeline'))
@app.route('/login', methods=['GET', 'POST'])
def login():
"""Logs the user in."""
if g.user:
return redirect(url_for('timeline'))
error = None
if request.method == 'POST':
user = query_db('''select * from user where
username = ?''', [request.form['username']], one=True)
if user is None:
error = 'Invalid username'
elif not check_password_hash(user['pw_hash'],
request.form['password']):
error = 'Invalid password'
else:
flash('You were logged in')
session['user_id'] = user['user_id']
return redirect(url_for('timeline'))
return render_template('login.html', error=error)
@app.route('/register', methods=['GET', 'POST'])
def register():
"""Registers the user."""
if g.user:
return redirect(url_for('timeline'))
error = None
if request.method == 'POST':
if not request.form['username']:
error = 'You have to enter a username'
elif not request.form['email'] or \
'@' not in request.form['email']:
error = 'You have to enter a valid email address'
elif not request.form['password']:
error = 'You have to enter a password'
elif request.form['password'] != request.form['password2']:
error = 'The two passwords do not match'
elif get_user_id(request.form['username']) is not None:
error = 'The username is already taken'
else:
db = get_db()
db.execute('''insert into user (
username, email, pw_hash) values (?, ?, ?)''',
[request.form['username'], request.form['email'],
generate_password_hash(request.form['password'])])
db.commit()
flash('You were successfully registered and can login now')
return redirect(url_for('login'))
return render_template('register.html', error=error)
@app.route('/api/receive_text', methods=['GET', 'POST'])
def receive_text():
"""receive a text message from Twilio."""
return
@app.route('/logout')
def logout():
"""Logs the user out."""
flash('You were logged out')
session.pop('user_id', None)
return redirect(url_for('public_timeline'))
# add some filters to jinja
app.jinja_env.filters['datetimeformat'] = format_datetime
app.jinja_env.filters['gravatar'] = gravatar_url
if __name__ == '__main__':
init_db()
app.run(host='127.0.0.1', debug=False, port=5000, ssl_context=('/Users/l/Development/Certificates/server.crt', '/Users/l/Development/Certificates/server.key'))
schema.sql文件:
create table user (
user_id integer primary key autoincrement,
username text not null,
email text not null,
pw_hash text not null
);
create table follower (
who_id integer,
whom_id integer
);
create table message (
message_id integer primary key autoincrement,
author_id integer not null,
text text not null,
pub_date integer
);
create table access_token (
user_id integer not null,
access_token_text text not null
);
create table completed_deliveries (
flower_order text not null,
shipping_address text not null,
total_cost real not null,
driver_name text not null,
estimated_delivery_time integer not null,
reputation text not null,
charge real not null,
arrival_estimation text not null,
picked_up_time text not null,
actual_arrival_time text not null
);
create table deliveries_in_progress (
flower_order text not null,
shipping_address text not null,
total_cost real not null,
driver_name text not null,
estimated_delivery_time integer not null,
reputation text not null,
charge real not null,
arrival_estimation text not null,
picked_up_time text not null
);
create table deliveries_awaiting_pickup (
flower_order text not null,
shipping_address text not null,
total_cost real not null,
driver_name text not null,
estimated_delivery_time integer not null,
reputation text not null,
charge real not null,
arrival_estimation text not null
);
create table deliveries_waiting_for_bids (
flower_order text not null,
shipping_address text not null,
total_cost real not null
);
答案 0 :(得分:1)
在这行deliveryies_awaiting_pickup():
return render_template('deliveries_awaiting_pickup.html', orders)
我应该明确命名每个变量,如下所示:
return render_template('deliveries_awaiting_pickup.html', orders=orders)
然后它奏效了。
另外,我注意到我的调试被意外设置为false。我把它设置为真如下。由于它是错误的,它只是给出了内部服务器错误&#34;而不是详细的调试报告。
app.run(host='127.0.0.1', debug=True, port=5000, ssl_context=('/Users/l/Development/Certificates/server.crt', '/Users/l/Development/Certificates/server.key'))