简体   繁体   中英

Flask BuildError as a result of url_for()

While my Flask program compiles, attempting to access the server through a browser causes the following build error.

Traceback (most recent call last):
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)    
  File "/var/www/pulleffect/pulleffect/middleware/reverse_proxy_fix.py", line 34, in __call__
    return self.app(environ, start_response)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/werkzeug/contrib/fixers.py", line 144, in __call__
    return self.app(environ, start_response)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/pulleffect/pulleffect/lib/utilities.py", line 97, in decorated_function
    return f(*args, **kwargs)
  File "/var/www/pulleffect/pulleffect/__init__.py", line 86, in index
    return render_template('index.html')
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/pulleffect/pulleffect/templates/index.html", line 1, in top-level template code
    {% extends "signin.html" %}
  File "/var/www/pulleffect/pulleffect/templates/signin.html", line 1, in top-level template code
    {% extends "layout.html" %}
  File "/var/www/pulleffect/pulleffect/templates/layout.html", line 35, in top-level 
template code
    var shiftsRoute = "{{ url_for('shifts.index') }}";
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/helpers.py", line 312, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/flask/helpers.py", line 305, in url_for
    force_external=external)
  File "/var/www/pulleffect/penv/lib/python2.7/site-packages/werkzeug/routing.py", line 1620, in build
    raise BuildError(endpoint, values, method)
BuildError: ('shifts.index', {}, None)

My understanding is that typically this comes from attemtping to call the url by its route, as opposed to its method. However, in this instance index is the method, not the route and the error is still raised.

This is the specific line within the program that fails:

var shiftsRoute = "{{ url_for('shifts.index') }}";

and this is the method index.

@shifts.route('')
@require_signin
def index():

unfortunately, I am quite lost. My only guess is that calling shifts.index, requires the index to be contained by the class shifts as opposed to simply being saved within shifts.py

What is shifts?

Simple Flask you should just do:

@route('')
def index():
    #do something
    pass

then:

url_for('index')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM