How to remove the language identifier from django-cms 2.4 URLs?

ぐ巨炮叔叔 提交于 2019-11-28 21:24:55

replace this pattern registration:

urlpatterns = i18n_patterns('',
 url(r'^admin/', include(admin.site.urls)),
 url(r'^', include('cms.urls')),
)

with this:

from django.conf.urls import patterns

urlpatterns = patterns('',
  url(r'^admin/', include(admin.site.urls)),
  url(r'^', include('cms.urls')),
)

The tutorial you pointed to uses the i18n_patterns method which does exactly this: prepends the language code to your urls.

Also note you can safely remove 'django.middleware.locale.LocaleMiddleware' and 'cms.middleware.language.LanguageCookieMiddleware' from your MIDDLEWARE_CLASSES if you will not use multiple languages.

@ppetrid's answer is still correct. However, as of Django 1.6 patterns is no longer available. Change the existing code to this:

from django.conf.urls import patterns

urlpatterns = (
  url(r'^admin/', include(admin.site.urls)),
  url(r'^', include('cms.urls')),
)

You will also get a warning if you leave the '', in the patterns too.

In django version 1.8.18 you just need to put False at this variable in settings.py

USE_I18N = False

USE_L10N = False

If you want to keep one language in the URL, for instance because you have backlinks in the web with the language code, you can simply take out the other language in settings.py

LANGUAGES = (        
    #('en', gettext('en')),
    ('de', gettext('de')),
)

CMS_LANGUAGES = {        
    'default': {
        'public': True,
        'hide_untranslated': False,
        'redirect_on_fallback': True,
    },
    1: [            
        {
            'public': True,
            'code': 'de',
            'hide_untranslated': False,
            'name': gettext('de'),
            'redirect_on_fallback': True,
        },
        # {
        #     'public': True,
        #     'code': 'en',
        #     'hide_untranslated': False,
        #     'name': gettext('en'),
        #     'fallbacks': ['de'],
        #     'redirect_on_fallback': True,
        # },
    ],
}

That way the URL still shows www.example.com/de/foo.html. In the Example above, that /de/ will be lost, which will render all your URLs in the web meaningless.

Thus, from an SEO perspective, it might not be the best option if you have already built up links with the language code in it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!