Search code examples
djangodjango-i18n

django KeyError for my i18n javascript catalog url


This is a bit odd. I'm using Django 1.9.6, and it doesn't like my i18n javascript_catalog url after I changed it from the obsolete 1.7 syntax (I finally decided I should do something about that "not gonna work in django 1.10" warning I get every time I runsrever). Here's what I've got:

urls.py

from django.views.i18n import javascript_catalog

js_info_dict = {
    'domain': 'djangojs',
    'packages': ('my_app',),
}

urlpatterns = [
    url(r'^jsi18n/(?P<packages>\S+?)/$', 
        javascript_catalog, js_info_dict, name='javascript_catalog'),

]

When trying to render:

KeyError at /my_app/my_url/
'packages'

I can't remove the 'packages' key from django_info_dict, because it causes NoReverseMatch error: Reverse for 'javascript_catalog' with arguments '()' and keyword arguments '{}' not found.

As you'd expect, here's where it occurs in the template.html:

<script src="{% url 'javascript_catalog' %}"></script>

This is pretty much copy-paste from the docs. I've got the django.views.i18n.javascript_catalog view and the name in the url ... what am I overlooking here?


Solution

  • I thought the 'packages' key referred to the one in the js_info_dict. In the error it actually referred to the capturing regex group named packages. The dynamic packages variable was not needed because I specified a static package my_app in the js_info_dict, hence the correct url pattern to use was:

    url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript_catalog'),