KeyError: ‘en-us’ when doing a reverse() lookup in urls.py

If you’re getting an error like this:


... in reverse_dict
return self._reverse_dict[language_code]
KeyError: 'en-us'

If your urls.py file contains a reverse() call, that is the reason.  You’re calling reverse before all the urls are loaded and everything is ready.  You need to use reverse_lazy() instead.:


url(r'^password/reset/$',
password_reset,
{'post_reset_redirect': reverse('password_reset_done'),
'html_email_template_name': 'registration/html_password_reset_email.html'},
name="password_reset"),

Change the reverse() call into a reverse_lazy() call and the problem will go away!

This entry was posted in django, python, Web Programming. Bookmark the permalink.

Comments are closed.