I'm currently trying to upgrade to Django 1.10.3
from Django 1.9.8
. Currently my tests are failing however, because for some reason Django is trying to translate/internationalize my URLs when I reverse()
them.
I've not changed anything when it comes to internationalization and would very much like to turn this "feature" off. Not only is it failing a lot of tests, but there are also some projects which have to refer to the URLs of this website statically. This means the URL on this website are not allowed to change (or I would have to edit them for every translation Django comes up with, which would be a real pain).
The actual error I'm encountering in my tests is the following:
Traceback (most recent call last):
File "/tests/unit/views/test_index.py", line 14, in setUp
self.url = reverse('indexpage')
File "/local/lib/python2.7/site-packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 346, in _reverse_with_prefix
possibilities = self.reverse_dict.getlist(lookup_view)
File "/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 243, in reverse_dict
return self._reverse_dict[language_code]
KeyError: 'nl-nl'
It's obvious Django is trying to do something with the language code provided in the settings for this project.
I have tried setting the USE_I18N
setting to False
. I have also tried this for USE_L10N
. I also tried removing the LANGUAGE_CODE
specified in my settings, but then the error thrown refers to the en-us
language code instead of the nl-nl
language code. I also tried installing the LocaleMiddleware
to see if I could at least get the error to stop showing, but unfortunately this also didn't work.
How can I turn off URL internalization/translation in Django. Or what would be a good alternative if this is not possible?
Thanks in advance.
It may be a red herring (caused by a different error). I had to work through several different exceptions that were being thrown when urls.py
files were loaded before this error went away. Try dropping into a django shell and running these commands:
from django.urls import reverse
reverse('indexpage')
and you should see the real error causing the problem there. Fix the problem and repeat until you have no more exceptions.
Use reverse_lazy
instead of reverse
on the changes that introduced the bug.
Looks like you're trying to get the url with reverse
before django registering it.
http://www.boxtricks.com/keyerror-en-us-when-doing-a-reverse-lookup-in-urls-py/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With