Search code examples
pythonflaskwerkzeug

AttributeError: 'NotFound' object has no attribute 'encode'


When a user goes on my flask app with a URL that has more than 3 sub paths, like "domain.com/var1/var2/var3/var4", it throws the error below:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1478, in    full_dispatch_request
response = self.make_response(rv)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1574, in make_response
rv = self.response_class(rv, headers=headers, status=status)
File "/usr/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 758, in __init__
self.status = status
File "/usr/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 862, in _set_status
self._status = to_native(value)
File "/usr/local/lib/python2.7/site-packages/werkzeug/_compat.py", line 111, in to_native
return x.encode(charset, errors)
AttributeError: 'NotFound' object has no attribute 'encode'

It works fine for url's that have less than or equal to 3 subpaths like "domain.com/var1/var2/var3", but I have routes for url's with 1, 2, and 3 subpaths.

How can I get my app to go to the 404 Page Not Found method instead of throwing this error?

Thank you!

edit.. Here's a pastebin of the route methods I'm using. http://pastebin.com/kjYsqk9n

Sorry I can't provide an example of this problem, it's really weird and I don't know how to reproduce it myself. My other flask apps work fine, this one doesn't. If anyone needs clarification, please let me know... thanks for all your help!


Solution

  • Found out why it was returning that error. It turns out I was passing in the error object instead of "404" at the end of this line:

    return render_template("error.html", usa=usa, canada=canada, breadcrumbs=breadcrumbs, error_code=e), 404
    

    Thanks!