Python flask admin RuntimeError: maximum recursion depth exceeded

Asked 2 years ago, Updated 2 years ago, 122 views

@babel.localeselector
def get_locale():
    print 'check2'
    if 'lang' in session:
        cur_lang = session['lang']
        if cur_lang in app.config['SUPPORTED_LANGUAGES'].keys():
            return cur_lang
        else:
            return request.accept_languages.best_match(app.config['SUPPORTED_LANGUAGES'].keys())
    else:
        best_lang = request.accept_languages.best_match(app.config['SUPPORTED_LANGUAGES'].keys())
        session['lang'] = best_lang
        return request.accept_languages.best_match(app.config['SUPPORTED_LANGUAGES'].keys())

Basically, I'm using the label for the web page implementation with flask. But if you go to the admin path, the function above runs indefinitely. RuntimeError: maximum recursion depth exceeded error occurs while check2 continues to be taken. In the normal path, it's a function that runs only once when you move a page, but why is that function taken indefinitely when you go to the admin page?

And @babel.localeselector, what does this mean when called?

python flask admin error

2022-09-22 19:35

1 Answers

Read the document at the site below for a detailed description of @babel.localeselector.

https://pythonhosted.org/Flask-Babel/

Simply put, the label is used for internationalization processing.

If you used the @babel.localeselector decorator, return locale information such as en and ko.

Check2 Being taken multiple times means that the function is not wrong, but it is calling get_locale several times.

Using pdb, set the brake pointer on the print 'check2' line and check the call stack when it stops.

If you can use ideas such as pycharm, it's easier to understand.

abcd.py

import pdb

def add(a, b):
    breakpoint() # python 3.7 or lower version should be pdb.set_trace()
    return a + b
def main():
    return add(3, 4)

if __name__ == '__main__':
    print(main())
(py37)  allinux@lghnh # python abcd.py
> /home/allinux/abcd.py(5)add()
-> return a + b
(Pdb) w
  /home/allinux/abcd.py(10)<module>()
-> print(main())
  /home/allinux/abcd.py(7)main()
-> return add(3, 4)
> /home/allinux/abcd.py(5)add()
-> return a + b
(Pdb)


2022-09-22 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.