Do not internationalize/translate URLs
Asked Answered
T

3

6

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.

Therefrom answered 10/11, 2016 at 11:58 Comment(3)
you can deactivate internationalization from settings.py itself: USE_I18N = True //set it to FalseVespid
@VijeshVenugopal I already tried this beforehand, but that doesn't work. I still receive the error I specified in my answer.Therefrom
This has nothing to do with translations, but it seems to be related to #26888. Can you show your urls and your test_index.py?Mcmillian
S
15

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.

Schrecklichkeit answered 16/2, 2017 at 23:44 Comment(1)
Please note that I've found that the actual error is usually somewhere at the top of all of the errors. So scrolling up to the very first few might actually give you an indication about what goes wrong. This might save you some messing around on CI places etc.Therefrom
A
1

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/

Appointment answered 19/11, 2019 at 18:51 Comment(0)
B
0

You need to set off the flag

USE_I18N = False

As suggested by Vijesh Venugopal and also see to it that your urls are included in the following segment:-

urlpatterns += [
    url("^", include("your_app.urls")),
]

Do not include them under these

urlpatterns = i18n_patterns(
    # Change the admin prefix here to use an alternate URL for the
    # admin interface, which would be marginally more secure.
    url("^admin/", include(admin.site.urls)),
)
Bimonthly answered 10/11, 2016 at 12:28 Comment(1)
Thanks, but unfortunately this doesn't work (already tried it beforehand). When I turn this off it still tried to resolve URLs with the language code. It still keeps throwing the error I specified in my question.Therefrom

© 2022 - 2024 — McMap. All rights reserved.