我正在使用以下内容设置 url 端点:
管理器.py
from xxx import ContactAPI
from xxx.models import Contact
# self.app is my Flask app
# self.session is SQLAlchemy Session
api_name = 'contact'
instance_endpoint = '/%s/<int:instid>' % api_name
methods = ['GET']
api_view = ContactAPI.as_view(api_name, self.session,
Contact, app)
self.app.add_url_rule(instance_endpoint, methods=methods,
defaults={'instid': None},
view_func=api_view)
get()
并在我的 ContactAPI 类中覆盖:
视图.py
from flask.views import MethodView
class ContactAPI(MethodView):
def __init__(self, session, model, app, *args, **kwargs):
super(ContactAPI, self).__init__(*args, **kwargs)
def get(self, instid):
print instid
当我点击 URL 时,/contact/1
我instid
打印为None
.
当我defaults={'instid': None},
从 manager.py 中删除该行时,我instid
打印为 1。
为什么在我的调用中有默认行来add_url_rule
覆盖我在我的 URL 中输入的内容?