Forums

Flask-Babel error with keys()

Hi guys, I get an error with Flask-Babel. In my get_locale() function I get this error: AttributeError: 'dict_keys' object has no attribute 'keys' for this line of code: return request.accept_languages.best_match( locales.keys(), app.config.get('BABEL_DEFAULT_LOCALE', 'en'))

I specify that I do not get this error in my IDE but only here.
How could I solve that?

My guess would be that you're using 2 different versions off Python between your IDE and PythonAnywhere. Make sure you're running your code with the same Python version and write the code to work in that version.

First of all, thank you for your answer.
Yes I just checked and in my IDE I use the latest release, that is 3.10.4.
How could I fix it to avoid rewriting the whole code?

Hmm, if you're using Python 3.x in both cases, that should be OK. What is the locales object that you're working with in that code?

The locales object is this:
locales = app.config.get('LANGUAGES', { 'en': 'English' }).keys()
And this is the configuration:
app.config['LANGUAGES'] = { 'en': 'English', 'it': 'Italiano' } app.config['BABEL_DEFAULT_LOCALE'] = 'en'

If you're getting the keys from that dictionary when you're assigning to locales, then getting the keys again in the function will not work because the locales variable will not contain a dictionary. I'm guessing that you should remove the keys() call from the line of code where you assign to locales.

I tried to remove the call to keys() but in this way it doesn't load the languages from my dictionary.

How does locales object looks like (dir(locales), vars(locales), type(locales))?