It's written in official documentation here that i18n_patterns()
is only allowed in your root URLconf.
This is a problem for me because I need those URLs working:
/en/products/
/fr/produits/
/sv/produkt/
/en/products/detail/[my product name]/
/en/produits/detail/[my product name]/
/sv/produkt/detalj/[my product name]/
/sv/produkt/detalj/[my product name]/
Heres my root urls.py
:
urlpatterns += i18n_patterns(
url(_(r'^produits/detail/'),
include('produits.urls', namespace="produits")
),
url(_(r'^produits/'),
include('produits.urls', namespace="produits")
),
)
So, for the lastest translation works ok, but the first one doesn't. The translation is ok, but I want to transfer the last part ('detail/'
) to the app produits
that should handle it transparently.
How do I do?
Here's how I've solved the problem. This is not a good solution but it's the only one working for now with Django 1.8.
I've removed my routes from my "produits
" application, and added them to the main urls.py
which means mixing application configuration with global configuration. I dont like it.
I've also added the name of the application before each route: produits_detail
and produits_index
. Thus I know the app those routes are for. Once again I dont like mixing "global setting" and specific settings, but it seems I have no choice. Anyway, few lines of code, and it works pretty well.
from django.contrib import admin
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _
from produits import views as p_views
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', include(admin.site.urls)),
]
urlpatterns += i18n_patterns(
url(_(r'^produits/detail/(?P<slug>[a-zA-Z0-9-_]+)/$'),
p_views.DetailView.as_view(), name='produits_detail'),
url(_(r'^produits/'),
p_views.IndexView.as_view(), name='produits_index'),
)