Home > other >  How to import a subfolder in the template directory while using blueprints and render_template and u
How to import a subfolder in the template directory while using blueprints and render_template and u

Time:02-04

I found this link but can't figure out how to use it for render_template and url_for when using blueprints.

Here is the templates and stripe_payment subfolder

Here is what I have tried.

I tried this below, userinfo is my blueprint

I assume this will work.

@userinfo.route('/donations')
def donations():
    return render_template('stripe_payment/donations.html', products=products)

Here is the link to donations route. I assume this is causing the error. How do I fix this?

<h2> <a href="{{ url_for ('stripe_payment/userinfo.donations') }}">Click Here to donate </a> </h2>

Here is the error

Traceback (most recent call last):
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\nmyle\OneDrive\Desktop\flaskcodeusethis\flaskblog2\app\userinfo\routes.py", line 77, in home
    return render_template('home.html', posts=posts, title='home')
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\templating.py", line 147, in render_template
    return _render(
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\templating.py", line 128, in _render
    rv = template.render(context)
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\jinja2\environment.py", line 1291, in render
    self.environment.handle_exception()
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\jinja2\environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "C:\Users\nmyle\OneDrive\Desktop\flaskcodeusethis\flaskblog2\app\templates\home.html", line 1, in top-level template code
    {% extends "layout.html" %}
  File "C:\Users\nmyle\OneDrive\Desktop\flaskcodeusethis\flaskblog2\app\templates\layout.html", line 21, in top-level template code
    <h2> <a href="{{ url_for ('stripe_payment/userinfo.donations') }}">Click Here to donate </a></h2>
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\helpers.py", line 338, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\flask\helpers.py", line 325, in url_for
    rv = url_adapter.build(
  File "C:\Users\nmyle\anaconda3\envs\py\Lib\site-packages\werkzeug\routing.py", line 2315, in build
    raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'stripe_payment/userinfo.donations'. Did you mean 'userinfo.login' instead?

CodePudding user response:

The parameter passed to url_for references the name of the blueprint followed by the identifier of the endpoint, separated by a period. The name of the blueprint is the first string passed when the instance is created. The subfolder doesn't matter.

url_for('userinfo.donations')

You only need to specify the subfolder within the template directory when calling render_template. Here you refer to the file to be rendered.

render_template('stripe_payment/donations.html', **locals())

  • Related