每当我尝试在本地运行我的程序时,我都没有任何问题,但是在 Elastic Beanstalk 上,当我尝试转到一个不涉及直接文本返回的 url 时,我得到 AttributeError: 'Flask' object has no attribute 'login_manager', / 和 / 可能有效,但 /teacher 无效。请忽略不好的编码想法,我只是快速地将它放在一起。所有模板都位于主应用程序下一级的模板文件夹中。
应用程序:
from flask import Flask, request, render_template, url_for, redirect
from flask.ext.login import LoginManager, UserMixin, login_required, login_user, flash, current_user
application = Flask(__name__)
application.debug=True
login_manager = LoginManager()
studentgrades={}
test=None
class Student():
def __init__(name, grade):
self.name=name
self.grade=grade
class Test():
def __init__(self, size, key):
self.size=size
self.key=key
def grade(self, request):
count=0.0
for k in request.form:
if k=='name':
pass
elif self.key.get(k)==request.form.get(k):
count+=1.0
return (count/(len(self.key)))*100
def length(self):
return self.size+1
class User(UserMixin):
def __init__(self, name, id, active=True):
self.name = name
self.id = id
self.active = active
def is_active(self):
return self.active
class Anonymous():
name = u"Anonymous"
USERS = {
1: User(u"Teacher", 1),
}
USER_NAMES = dict((u.name, u) for u in USERS.itervalues())
@login_manager.user_loader
def load_user(id):
return USERS.get(int(id))
@application.route('/maybe')
def mayb():
return 'huh'
@application.route('/')
def main():
global test
if test is None:
return 'No Test Loaded'
return render_template('testtake.html', i=test.length())
@application.route("/teacher")
def logins():
if current_user.is_active()==True:
return render_template("teachon.html")
if request.method =='POST':
if request.form.get('Password')=='x':
if login_user(USER_NAMES[request.form.get("username")]):
flash('Logged in Successfully.')
return render_template("teachon.html")
else:
flash('Sorry, couldn\'nt login')
return render_template("login.html")
@application.route('/teststart')
@login_required
def testcount():
return render_template('teacherlog.html')
@application.route("/logout")
@login_required
def logout():
logout_user()
flash("Logged out.")
return redirect(url_for("index"))
@application.route('/submit/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
studentgrades[request.form.get('name')]=test.grade(request)
return 'Test submitted'
else:
return 'GET on login not supported'
@application.route('/keytaker/', methods=['POST'])
@login_required
def keytake():
key={}
for i in request.form:
key[i]=request.form.get(i)
z=len(request.form)
global test
test=Test(z, key)
flash('Submitted')
return render_template('teachon.html')
@application.route('/keymaker/', methods=['GET', 'POST'])
@login_required
def teachlog():
if request.method == 'POST':
x= int(request.form.get('number'))
return render_template("keymaker.html", i=x)
@application.route('/grades/')
@login_required
def viewgrades():
global studentgrades
sgrades={}
u=studentgrades.iterkeys()
for i in studentgrades.iterkeys():
sgrades[unicode(i)]=unicode(studentgrades[i])
q=sgrades.iterkeys()
return render_template("gradeview.html", grades=sgrades, z=len(sgrades), x=q)
if __name__ == "__main__":
application.secret_key = 'removed'
login_manager.init_app(application)
application.run()
要求.txt:
Flask==0.10.1
Flask-Login==0.2.7
Flask-OAuth==0.12
Jinja2==2.7.1
MarkupSafe==0.18
Werkzeug==0.9.4
httplib2==0.8
itsdangerous==0.23
oauth2==1.5.211
python.config(在 .ebextensions 文件夹中):
option_settings:
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"