Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not internationalize/translate URLs

Problem

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).


Error

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.


What have I tried

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.


Question

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.

like image 727
Bono Avatar asked Nov 10 '16 11:11

Bono


2 Answers

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.

like image 182
Brad Pitcher Avatar answered Sep 19 '22 01:09

Brad Pitcher


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/

like image 44
Julio Marins Avatar answered Sep 20 '22 01:09

Julio Marins