Flask error werkzeug.routing.RequestRedirect: 301 Moved Permanently: None

Viewed 360

I have a flask app that is giving me the below error when I try to access it using https://server/app. I have other apps that are not giving me this error. I can access the application if I give it a trailing slash (https://server/apps/). I am running Apache 2.4 with mod_wsgi. Flask version is 1.0.2.

Here is my app route code

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        data = {}
        end = datetime.datetime.strptime(request.form.get('start_date'),
                                         '%Y-%m-%d').date()
        start = end - relativedelta(days=30)
        inventory_stats = get_julian_days(end, start)
        start_send = start.strftime('%Y/%m/%d')
        end_send = end.strftime('%Y/%m/%d')
        data['start_date'] = start_send
        data['end_date'] = end_send
        data['inventory'] = inventory_stats
        return json.dumps(data)
    else:
        end = datetime.date.today()
        start = end - relativedelta(days=30)
        inventory_stats = get_julian_days(end, start)
        g.cursor.execute(queries.get_last_updated)
        last_updated = g.cursor.fetchone()
        return render_template('index.html', inventory_stats=inventory_stats,
                               start=start, end=end, last_updated=last_updated)

Here is the error I am getting.

Traceback (most recent call last):
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/werkzeug/routing.py", line 1538, in match
    rv = rule.match(path, method)
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/werkzeug/routing.py", line 776, in match
    raise RequestSlash()
werkzeug.routing.RequestSlash

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/flask/app.py", line 1791, in dispatch_request
    self.raise_routing_exception(req)
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/flask/app.py", line 1774, in raise_routing_exception
    raise request.routing_exception
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/flask/ctx.py", line 336, in match_request
    self.url_adapter.match(return_rule=True)
  File "/opt/ecappdev/.edm/envs/dev365/lib/python3.6/site-packages/werkzeug/routing.py", line 1542, in match
    safe='/:|+') + '/', query_args))
werkzeug.routing.RequestRedirect: 301 Moved Permanently: None

If I comment out the following code the application automatically redirects and does not error. Why would this code cause this problem?

@app.errorhandler(Exception)
def handle_app_error(exception):
    ''' Handle any potential errors'''
    LOGGER.error(exception, exc_info=True)
    return render_template('error.html')

I found my problem. I am also overwriting HTTPException. I need to pass on this exception. This code works.

@app.errorhandler(Exception)
def handle_exception(exception):
    # pass through HTTP errors
    if isinstance(exception, HTTPException):
        return exception

    # handling non-HTTP exceptions only
    LOGGER.error(exception, exc_info=True)
    return render_template('error.html')
0 Answers
Related