如何从django-cms 2.4 URL中删除语言标识符?

时间:2021-07-14 08:51:33

I have followed the tutorial to make a new Django-CMS (2.4) site. I am only using a single language (English).

我按照教程制作了一个新的Django-CMS(2.4)网站。我只使用一种语言(英语)。

There is an automatic redirect to include the language identifier '/en/' in my site's URLs. How do I remove it?

有一个自动重定向,在我网站的网址中包含语言标识符'/ en /'。我该如何删除它?

thanks.

谢谢。

4 个解决方案

#1


28  

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.

您指向的教程使用i18n_patterns方法,它完全按照以下方式执行:将语言代码添加到您的URL。

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.

另请注意,如果您不使用多种语言,可以安全地从MIDDLEWARE_CLASSES中删除“django.middleware.locale.LocaleMiddleware”和“cms.middleware.language.LanguageCookieMiddleware”。

#2


3  

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

@ppetrid的答案仍然是正确的。但是,从Django 1.6开始,模式不再可用。将现有代码更改为:

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.

如果你也在模式中留下'',你也会收到警告。

#3


2  

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

在django版本1.8.18中,您只需要在settings.py中将此变量放入False

USE_I18N = False

USE_L10N = False

#4


1  

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

如果您想在URL中保留一种语言,例如因为您在网络中有语言代码的反向链接,您只需在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.

这样,网址仍会显示www.example.com/de/foo.html。在上面的示例中,/ de /将丢失,这将使Web中的所有URL无意义。

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.

因此,从SEO的角度来看,如果您已经在其中构建了包含语言代码的链接,那么它可能不是最佳选择。

#1


28  

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.

您指向的教程使用i18n_patterns方法,它完全按照以下方式执行:将语言代码添加到您的URL。

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.

另请注意,如果您不使用多种语言,可以安全地从MIDDLEWARE_CLASSES中删除“django.middleware.locale.LocaleMiddleware”和“cms.middleware.language.LanguageCookieMiddleware”。

#2


3  

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

@ppetrid的答案仍然是正确的。但是,从Django 1.6开始,模式不再可用。将现有代码更改为:

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.

如果你也在模式中留下'',你也会收到警告。

#3


2  

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

在django版本1.8.18中,您只需要在settings.py中将此变量放入False

USE_I18N = False

USE_L10N = False

#4


1  

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

如果您想在URL中保留一种语言,例如因为您在网络中有语言代码的反向链接,您只需在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.

这样,网址仍会显示www.example.com/de/foo.html。在上面的示例中,/ de /将丢失,这将使Web中的所有URL无意义。

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.

因此,从SEO的角度来看,如果您已经在其中构建了包含语言代码的链接,那么它可能不是最佳选择。