我正在尝试在测试Flask应用程序中使用蓝图,但遇到了一个奇怪的问题。这是我的代码的相关部分:
from functools import wraps
from flask import flash, redirect, render_template, \
request, session, url_for, Blueprint
from sqlalchemy.exc import IntegrityError
from datetime import datetime
from time import localtime, strftime
from .forms import AddAppointmentForm
from project import db
from project.models import Appointment
appointments_blueprint = Blueprint('appointments', __name__)
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('users.login'))
return wrap
@appointments_blueprint.route('/appointments/')
@login_required
def appointments():
# Get current date
current_datetime = strftime("%Y-%m-%d %H:%M:%S", localtime())
future_appointments = db.session.query(Appointment)\
.filter(Appointment.due_date >= current_datetime)\
.order_by(Appointment.due_date.asc())
past_appointments = db.session.query(Appointment)\
.filter(Appointment.due_date < current_datetime)\
.order_by(Appointment.due_date.asc())
return render_template('appointments.html',
form = AddAppointmentForm(request.form),
future_appointments=future_appointments,
past_appointments=past_appointments)
现在,当我运行该应用程序时,出现以下错误:
File "/home/mosquito/git/flask-scheduler/project/appointments/views.py", line 72, in <module>
@login_required
File "/home/mosquito/python_envs/flask-scheduler/local/lib/python2.7/site-packages/flask/blueprints.py", line 160, in decorator
endpoint = options.pop("endpoint", f.__name__)
AttributeError: 'NoneType' object has no attribute '__name__'
查看blueprints.py,我看到以下内容:
def route(self, rule, **options):
"""Like :meth:`Flask.route` but for a blueprint. The endpoint for the
:func:`url_for` function is prefixed with the name of the blueprint.
"""
def decorator(f):
endpoint = options.pop("endpoint", f.__name__)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
这失败,因为'f'为None。现在更有趣的是,如果我删除
@login_required
装饰器,当它到达那个点时,f是一个函数,所以没关系。
另一方面,如果我删除
@appointments_blueprint.route()
装饰器,它也可以工作。因此,看起来这两个装饰器的组合导致f为None ....任何想法,这是怎么回事?
您需要取消缩进return
:
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('users.login'))
return wrap
您使其成为wrap
函数本身的一部分,因此外部装饰器函数未返回任何内容。
删除@appointments_blueprint.route()
简单路线不会注册路线,因此您永远不会发现自己设置appointments
为None
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句