mongodb / flask-pymongo

PyMongo support for Flask applications
BSD 2-Clause "Simplified" License
721 stars 175 forks source link

the converter 'ObjectId' does not exist #152

Closed Joe-Heffer-Shef closed 2 years ago

Joe-Heffer-Shef commented 2 years ago

I'm trying to use the ObjectId URL routing map type converter: https://flask-pymongo.readthedocs.io/en/latest/#flask_pymongo.BSONObjectIdConverter

When I do this, there's an error saying that this ObjectId converter doesn't exist. However, it does seem to be present in app.url_map.converters when I inspect this when the view is loaded.

This is the Flask view:

@blueprint.route('/<ObjectId:option_id>')
def detail(option_id):
    options = app.mongo.db.options  # type: flask_pymongo.wrappers.Collection
    option = options.find_one_or_404(option_id)
    return flask.render_template('option/detail.html', option=option)

This is the error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_proc
    self.load_wsgi()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiap
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.10/site-packages/gunicorn/util.py", line 412, in import_app
    app = app(*args, **kwargs)
  File "/opt/owast/owast/app_factory.py", line 18, in create_app
    register_blueprints(app)
  File "/opt/owast/owast/app_factory.py", line 58, in register_blueprints
    app.register_blueprint(owast.blueprints.option.views.blueprint)
  File "/usr/local/lib/python3.10/site-packages/flask/scaffold.py", line 57, in wrapper_func
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1028, in register_blueprint
    blueprint.register(self, options)
  File "/usr/local/lib/python3.10/site-packages/flask/blueprints.py", line 370, in register
    deferred(state)
  File "/usr/local/lib/python3.10/site-packages/flask/blueprints.py", line 421, in <lambda>
    lambda s: s.add_url_rule(
  File "/usr/local/lib/python3.10/site-packages/flask/blueprints.py", line 103, in add_url_rule
    self.app.add_url_rule(
  File "/usr/local/lib/python3.10/site-packages/flask/scaffold.py", line 57, in wrapper_func
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1086, in add_url_rule
    self.url_map.add(rule)
  File "/usr/local/lib/python3.10/site-packages/werkzeug/routing.py", line 1540, in add
    rule.bind(self)
  File "/usr/local/lib/python3.10/site-packages/werkzeug/routing.py", line 799, in bind
    self.compile()
  File "/usr/local/lib/python3.10/site-packages/werkzeug/routing.py", line 874, in compile
    _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  File "/usr/local/lib/python3.10/site-packages/werkzeug/routing.py", line 863, in _build_regex
    convobj = self.get_converter(variable, converter, c_args, c_kwargs)
  File "/usr/local/lib/python3.10/site-packages/werkzeug/routing.py", line 813, in get_converter
    raise LookupError(f"the converter {converter_name!r} does not exist")
LookupError: the converter 'ObjectId' does not exist
Joe-Heffer-Shef commented 2 years ago

Never mind, I was loading the views (i.e. registering the Flask blueprints) before loading flask-pymongo.