wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
222 stars 84 forks source link

Redirections to second language are broken #683

Open HappyMiner opened 1 year ago

HappyMiner commented 1 year ago

Using

Django 4.1.7 Python 3.10.6 wagtail 4.2 wagtail_localize 1.5

redirections to a second language are broken.

Opening http://127.0.0.1:8000/ correctly redirects to http://127.0.0.1:8000/en/ however, when I click "live view" in the admin for the Chinese version of my page I am being redirected to http://127.0.0.1:8000/en/cn/ which is obviously wrong. I searched online for 2 days without finding any explanation or solution.

Steps to reproduce

Follow the steps in the documentation: https://www.wagtail-localize.org/ https://www.wagtail-localize.org/tutorial/2-configure-wagtail-localize/

My files for reference

urls.py

from django.conf import settings
from django.conf.urls.i18n import i18n_patterns

from django.urls import include, path
from django.contrib import admin

from wagtail.admin import urls as wagtailadmin_urls
from wagtail import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls

from search import views as search_views

urlpatterns = [
    path("django-admin/", admin.site.urls),
    path("admin/", include(wagtailadmin_urls)),
    path("documents/", include(wagtaildocs_urls)),
]

if settings.DEBUG:
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    # Serve static and media files from development server
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns = urlpatterns + i18n_patterns(
    # Wagtail_localize req.
    path("search/", search_views.search, name="search"),
    # For anything not caught by a more specific rule above, hand over to
    # Wagtail's page serving mechanism. This should be the last pattern in
    # the list:
    path("", include(wagtail_urls)),
    # Alternatively, if you want Wagtail pages to be served from a subpath
    # of your site, rather than the site root:
    #    path("pages/", include(wagtail_urls)),
    # prefix_default_language = False,
)

base.py

"""
Django settings for website project.

Generated by 'django-admin startproject' using Django 4.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

WAGTAILEMBEDS_RESPONSIVE_HTML = True

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# Adding support for review of content on local LAN.
ALLOWED_HOSTS = ["localhost", "192.168.1.6"]

# Application definition

INSTALLED_APPS = [
    "home",
    "menus",
    "resources",
    "courses",
    "contact",
    "schedule",
    "search",

    # Wagtail localize
    "wagtail_localize",
    "wagtail_localize.locales",
    # End Wagtail localize

    "wagtail.contrib.forms",
    "wagtail.contrib.redirects",
    "wagtail.embeds",
    "wagtail.sites",
    "wagtail.users",
    "wagtail.snippets",
    "wagtail.documents",
    "wagtail.images",
    "wagtail.search",
    "wagtail.admin",
    "wagtail",

    "modelcluster",
    "taggit",

    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_extensions",
]

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "django.middleware.security.SecurityMiddleware",
    # Wagtail_localize req.
    "django.middleware.locale.LocaleMiddleware",
    "wagtail.contrib.redirects.middleware.RedirectMiddleware",
]

ROOT_URLCONF = "website.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            os.path.join(PROJECT_DIR, "templates"),
        ],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "website.wsgi.application"

# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = "en"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

# Wagtail localize req.
WAGTAIL_I18N_ENABLED = True

USE_TZ = True

WAGTAIL_CONTENT_LANGUAGES = LANGUAGES = [
    ("en", "English"),
    ("cn", "Chinese"),
]

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, "static"),
]

# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# JavaScript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
# See https://docs.djangoproject.com/en/4.1/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"

# Wagtail settings

WAGTAIL_SITE_NAME = "website"

# Search
# https://docs.wagtail.org/en/stable/topics/search/backends.html
WAGTAILSEARCH_BACKENDS = {
    "default": {
        "BACKEND": "wagtail.search.backends.database",
    }
}

# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
WAGTAILADMIN_BASE_URL = "http://example.com"
HappyMiner commented 1 year ago

I just tried again with an empty database, just in case there was something that it does not agree with, no change.

HappyMiner commented 1 year ago

I have made a final attempt before I bury this for good. Created a brand new project according to the documentation. Under "Check the site" it is stated: "In either case, you can view the site in /en/ or /fr/ (no differences yet)." Which is WRONG. The above mentioned redirect also happens in this new base project. If I try to go to http://127.0.0.1:8000/cn/ I get an automatic redirection to http://127.0.0.1:8000/en/cn/ Creating the CN locale in the admin also does not change anything.

zerolab commented 1 year ago

I have a local set up of bakerydemo with pretty much the same settings. The only difference I have is RedirectMiddleware is before LocaleMiddleware.

Best to join the Wagtail Slack and ask in #support or #multi-language

HappyMiner commented 1 year ago

I have a local set up of bakerydemo with pretty much the same settings. The only difference I have is RedirectMiddleware is before LocaleMiddleware.

Best to join the Wagtail Slack and ask in #support or #multi-language

Many thanks for your answer! According to the documentation LocaleMiddleware should come BEFORE RedirectMiddleware. If this is incorrect it should be urgently corrected or clarified.

image

enzedonline commented 1 year ago

I don't think this is it, I have LocaleMiddleware before RedirectMiddleware on my sites. Everything works as expected.

zerolab commented 1 year ago

I didn't think it was it, but was one differece I noticed between my dev setup and the settings shared here. I'm afraid I do not have the capacity to help with this for a while, so Slack is the best bet

enzedonline commented 1 year ago

@zerolab I'm unable to spare any time for the foreseeable future to help myself too unfortunately

@HappyMiner You're base.py looks fine, it checks with what I've used on other sites I've created. I presume you set up your locales in the admin interface as well? You should see something like this:

image

And then to view an /en/page with /cn/page, then page needs to be translated from the admin drop down list ... e.g.

image

Maybe you've done all this already, just wanted to double check.

I have a sandbox project for testing various bits of codes, it's set up with 4 languages and working as expected. Feel free to clone it or look at the set up to check it out. https://github.com/enzedonline/testlocalize

HappyMiner commented 1 year ago

@enzedonline Thank you for your answer. Yes, I also did the translations in the admin section from the beginning. No change. Yesterday I found that when following along the general Wagtail docs I encounter the same issue (without using wagtail_localize). So my guess is that the redirecting functions ("django.middleware.locale.LocaleMiddleware", and / or "wagtail.contrib.redirects.middleware.RedirectMiddleware",) must have issues. I mean there isn't really anything I can misconfigure when following the tutorials... Especially on a brand new project as described in the docs.

enzedonline commented 1 year ago

I think this is far more likely something to do with your local environment (either local or global) since it works as described in the docs for others.

I have several wagtail-localize sites using different locale sets, on mtiple platforms. Never seen this issue before.

If you try the site I linked to earlier and it doesn't work in your environment then that's where I'd be looking to see what doesn't square up.

zerolab commented 11 months ago

@HappyMiner have you tried the advice from https://github.com/wagtail/wagtail-localize/issues/683#issuecomment-1489974737 ?