Search code examples
pythondjangodjango-cms

Django-cms admin url 404 after language change


I recently started learning Django & Django CMS, and was trying to change the language of my website from English to Dutch, I migrated over my pages using

Page.objects.filter(languages='en').update(languages='nl')
Title.objects.filter(language='en').update(language='nl')
CMSPlugin.objects.filter(language='en').update(language='nl')

In my settings.py I also changed LANGUAGE_CODE to 'nl' and set my languages and CMS languages as follows

LANGUAGES = (
    ## Customize this
    ('nl', 'Nederlands'),
)

CMS_LANGUAGES = {
    ## Customize this
    1: [
        {
            'code': 'nl',
            'name': 'Nederlands',
            'redirect_on_fallback': True,
            'public': True,
            'hide_untranslated': False,
        },
    ],
    'default': {
        'redirect_on_fallback': True,
        'public': True,
        'hide_untranslated': False,
    },
}

This works like a charm for the normal pages, but as soon as I try to open the admin interface I get the following error

Request Method: GET
Request URL:    http://localhost:8000/nl/en/admin/cms/page/?language=en
Raised by:  cms.views.details
Using the URLconf defined in PinManagementSite.urls, Django tried these URL patterns, in this order:

sitemap.xml
nl/ admin/
nl/ ^cms_login/$ [name='cms_login']
nl/ ^cms_wizard/
nl/ ^(?P<slug>[0-9A-Za-z-_.//]+)/$ [name='pages-details-by-slug']
nl/ ^$ [name='pages-root']
^media/(?P<path>.*)$
^static/(?P<path>.*)$
The current path, /nl/en/admin/cms/page/, didn't match any of these.

As far as I can see I configured it all correctly, but when I go to the admin pages it tries to route me through /nl/en instead of just /nl/ and I can't figure out why.

For completeness I'll add my urls.py below

from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
from django.urls import include, path

admin.autodiscover()

urlpatterns = [
    path("sitemap.xml", sitemap, {"sitemaps": {"cmspages": CMSSitemap}}),
]


urlpatterns += i18n_patterns(
    path("admin/", admin.site.urls),
    path("", include("cms.urls")),
)

# This is only needed when using runserver.
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Versions are djangocms==3.8.0 & Django==3.1.8


Solution

  • You are using only one language, so don’t use i18n_patterns().

    urlpatterns += [
        path(r'^admin/', admin.site.urls),
        path(r'^', include('cms.urls')),
    ]

    You are trying to access '/en/admin/cms/page/', but language en not added to your LANGUAGES.

    So Django try adding default language prefix nl and redirecting to that url in your case 'nl/en/admin/cms/page/'. In this case the path not longer matches with the admin path.


    You can also try to add English in your LANGUAGES:

    from django.utils.translation import ugettext_lazy as _
    
    LANGUAGES = (
        ('nl', _('Nederlands')),
        ('en', _('English')),
    )

    I tried your settings. Django CMS works fine with the same settings as yours. Be sure the page is not cached.