Issue
Im trying to create an invite url for a project I'm working on and I have seen a few versions on line when it comes to creating a token URL for that to work.
I have an error when it comes to creating a URL
^^^^^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/flask/app.py", line 1071, in url_for
return self.handle_url_build_error(error, endpoint, values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/flask/app.py", line 1060, in url_for
rv = url_adapter.build( # type: ignore[union-attr]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/werkzeug/routing/map.py", line 919, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'add_user_confirm' with values ['token']. Did you mean 'users_blueprint.add_user_confirm' instead?
I have two routes under a blueprint which look as follows:
@users_blueprint.route("/invite_user", methods=["GET", "POST"])
def invite_user():
email = request.json["email"]
if email:
ts = URLSafeTimedSerializer("JWT_SECRET_KEY")
salt = "12345"
token = ts.dumps(email, salt)
confirm_url = url_for("add_user_confirm", token=token, _external=True)
try:
msg = Message(
"Welcome to TAGR",
sender="[email protected]",
html="You have been invited to set up an account on PhotogApp. Click here: "
+ confirm_url,
recipients=[email],
)
mail.send(msg)
except Exception as e:
print(e)
@users_blueprint.route("/add_user_confirm/<token>", methods=["GET", "POST"])
def add_user_confirm(token):
print(token)
Im new to this way of working and would welcome some help
Solution
You are trying to make a url for a route within your users_blueprint
blueprint. Flask blueprint routes are defined in the format, <blueprint_name>.<route_name>
. This means, to access a route within your blueprint, you would need to modify the following line,
confirm_url = url_for("add_user_confirm", token=token, _external=True)
to this:
confirm_url = url_for("users_blueprint.add_user_confirm", token=token, _external=True)
Without blueprints, you would not need to put the blueprint name before the route, and url_for("add_user_confirm", ...)
would be fine. But because you defined the 'add_user_confirm' route under a blueprint with @users_blueprint.route(...)
instead of @app.route(...)
, you need to specify this blueprint whenever you want to access that route.
Answered By - Patrick Yoder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.