2

I know how to add a general prefix to all urls thanks to this answer https://stackoverflow.com/a/54364454/2219080. But I want to have urls where we have the added prefix always coming before the language prefix (added by i18n_patterns ).

from django.urls import include, path, re_path
from django.views.generic import TemplateView
from django.conf.urls.i18n import i18n_patterns


urlpatterns = [
    path("grappelli/", include("grappelli.urls")),  # grappelli URLS
    path("admin/doc/", include("django.contrib.admindocs.urls")),
    path("admin/", admin.site.urls),
    path("pages/", include("django.contrib.flatpages.urls")),
    re_path(r"^api-auth/", include("rest_framework.urls")),
    re_path(r"^accounts/", include("allauth.urls")),
    re_path(r"^indicators/", include("indicators.urls", namespace="indicators")),
    path("", TemplateView.as_view(template_name="landing.html"), name="landing"),
]


if settings.URL_PREFIX:
    urlpatterns = [path(r"{}".format(settings.URL_PREFIX), include(urlpatterns))]

E.g: Having this code above, I would like to have an url https://example.com/mycuteprefix/en/indicators/ instead of the usual https://example.com/en/mycuteprefix/indicators/.

Whenever I apply internationalization to the first urlpatterns I get an error. For example if I try this one bellow:

urlpatterns = [
    path("grappelli/", include("grappelli.urls")),  # grappelli URLS
    path("admin/doc/", include("django.contrib.admindocs.urls")),
    path("admin/", admin.site.urls),
    path("pages/", include("django.contrib.flatpages.urls")),
    re_path(r"^api-auth/", include("rest_framework.urls")),
    re_path(r"^accounts/", include("allauth.urls")),
    re_path(r"^indicators/", include("indicators.urls", namespace="indicators")),
]

urlpatterns += i18n_patterns(
    path("", TemplateView.as_view(template_name="landing.html"), name="landing"),
    )

if settings.URL_PREFIX:
    urlpatterns = [path(r"{}".format(settings.URL_PREFIX), include(urlpatterns))]

I have this error:

urlpatterns = [path(r"{}".format(settings.URL_PREFIX), include(urlpatterns))] File "/home/username/.virtualenvs/roject/lib/python3.5/site-packages/django/urls/conf.py", line 52, in include 'Using i18n_patterns in an included URLconf is not allowed.' django.core.exceptions.ImproperlyConfigured: Using i18n_patterns in an included URLconf is not allowed.

How to achieve that ?

iMitwe
  • 1,218
  • 16
  • 35

0 Answers0