wagtail / wagtail-localize

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

Error 404 when translating model through TranslatableModelAdmin #660

Open unreadableusername opened 1 year ago

unreadableusername commented 1 year ago

Hi everyone,

I have a problem where an error 404 occurs when I try to translate my custom model with the translate button in the custom TranslatableModelAdmin entry. If you need more information, don't hesitate to ask :)

For the project wagtail 4.1.1, wagtail-localize 1.3.3 and Django 4.1.3 where used.

image

image

This is the console output:

Not Found: /admin/localize/modeladmin/submit/app_models/employeemodel/1/
[30/Nov/2022 11:32:42] "GET /admin/localize/modeladmin/submit/app_models/employeemodel/1/ HTTP/1.1" 404 7710
[30/Nov/2022 11:32:42] "GET /admin/jsi18n/ HTTP/1.1" 200 6681

wagtailhooks.py:

from wagtail.contrib.modeladmin.options import ModelAdminGroup, modeladmin_register
from wagtail_localize.modeladmin.options import TranslatableModelAdmin
from app_models.employee_model import EmployeeModel

class MitarbeiterAdmin(TranslatableModelAdmin):
    model = EmployeeModel
    base_url_path = 'mitarbeiter-profile' # customise the URL from default to admin/mitarbeiter-profile
    menu_icon = 'group'
    list_display = ('fullname', 'organisationseinheit')

    def fullname(self, obj):
        return ("%s %s" % (obj.first_name, obj.last_name))

    fullname.short_description = 'Name'
    list_filter = ('organisationseinheit',)
    search_fields = ('first_name', 'last_name')

# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(MitarbeiterAdmin)

employee_model.py - omitted panels for readability

from django.db import models
from app_models.organizational_units import ORGANISATIONSEINHEITEN
from wagtail.admin.panels import FieldPanel, FieldRowPanel

from wagtail.models import TranslatableMixin

class EmployeeModel(TranslatableMixin, models.Model):
    first_name = models.CharField(max_length=255, blank=False, null=False)
    last_name = models.CharField(max_length=255, blank=False, null=False)
    image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
    organisationseinheit = models.CharField(
        max_length=150,
        choices=ORGANISATIONSEINHEITEN,
        default=None,
        null=False,
        blank=False
    )
    job_description = models.CharField(max_length=255, blank=False, null=False)

    location_choice = models.CharField(
        max_length=50,
        choices=[
            ('Ostufer', 'Ostufer'),
            ('Westufer', 'Westufer'),
        ],
        default=None,
        null=False,
        blank=False
    )
    office = models.CharField(max_length=255, blank=False, null=False, default=None)
    webex_url = models.URLField(null=True, blank=True)

    panels = [
       ...
    ]

    def __str__(self):
        return self.first_name + " " + self.last_name

    class Meta(TranslatableMixin.Meta):     # noqa
        verbose_name = "Mitarbeiter Profil"
        verbose_name_plural = "Mitarbeiter Profile"

Base.py with apps, i18n and locale 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)

# Application definition

INSTALLED_APPS = [
    "home",
    "search",
    "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",

    "app_models",
    "wagtail_localize",                 # Enable https://www.wagtail-localize.org/
    "wagtail_localize.modeladmin",
    "wagtail.contrib.modeladmin",       # allows to modify ModelAdmin
    "wagtail_localize.locales",
]

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.locale.LocaleMiddleware",                        # necessary for language detection
    "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.contrib.redirects.middleware.RedirectMiddleware",
]

ROOT_URLCONF = "intranet.urls"

TEMPLATES = [
   ... omitted - not changed from default
]

WSGI_APPLICATION = "intranet.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 = [
   ... omitted - not changed from default
]

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

LANGUAGE_CODE = "de-de"

TIME_ZONE = "CET"

USE_I18N = True
WAGTAIL_I18N_ENABLED = True

USE_L10N = True

USE_TZ = True

LANGUAGES = [
    ('de-DE', "Deutsch"),
    ('en-US', "English (United States)"),
]

WAGTAIL_CONTENT_LANGUAGES = LANGUAGES = [
    ('de', "Deutsch"),
    ('en', "Englisch")
]

# 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 = "intranet"

# 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"