Mezzanine 4.2.2 / 4.2.3: Search breaks when USE_MODELTRANSLATION = True

157 views Asked by At

I've been having quite the time trying to get my search functionality to work correctly on my mezzanine project using django-modeltranslation. I am fairly new to Django, Mezzanine, and Python as well so no surprise as to why I'm having difficulty figuring this issue out.

My translations work fine. No issues there. However, each time I set USE_MODELTRANSLATION = True in my settings.py and perform a search query I just get re-directed to my homepage each time rather than the expected search results page and in my console output I'll see "POST /i18n/ HTTP/1.1" 302 0.

For the latter, if I set USE_MODELTRANSLATION = False and perform a search query I get the expected search result and in my output "GET /search/?q=test HTTP/1.1" 200 12972.

I also noticed that each POST is also passing the language parameter in the headers which I suspect to be part of the problem. I also suspected some issues with my urls.py and attempted many combinations specific to the search url but didn't have any luck with that either. I am almost certain the issue may be with modeltranslation set_language.

So far I've tested my scenario with the following combinations but no resolve:

  1. Mezzanine 4.2.3 | Dango 1.11 | django-modeltranslation 0.12
  2. Mezzanine 4.2.0 | Dango 1.10 | django-modeltranslation 0.12
  3. Mezzanine 4.1.0 | Dango 1.10 | django-modeltranslation 0.11
  4. Mezzanine 4.0.1 | Dango 1.9.12 | django-modeltranslation 0.11
  5. Mezzanine 4.2.2 | Dango 1.10.8 | django-modeltranslation 0.12 (Currently on this one)

I have also included the following patches in my current setup as I did run into issues syncing translation fields and running python manage.py createdb:

https://github.com/stephenmcd/mezzanine/commit/c244b603a6efab5062dcf97b1e12227e61ba4fb8 https://github.com/stephenmcd/mezzanine/pull/1764/files

If anybody could point me in the right direction as to resolve the search functionality with Mezzanine and django-modeltranslation it would be VERY much appreciated!


My models.py and views.py are bare as I am just trying to figure out this issue at the moment. I am not doing anything fancy in there anyways.

With my translation.py I still need to clean up my old imports, but currently only has what I need to translate the fields for pinax testimonials:

from modeltranslation.translator import translator, TranslationOptions
from mezzanine.core.translation import (TranslatedDisplayable, TranslatedRichText)
from mezzanine.pages.models import Page
from mezzanine.core.models import RichText, Orderable, Slugged
from modeltranslation.translator import translator, TranslationOptions
from django.db import models
from pinax.testimonials.models import Testimonial
from django.utils import timezone

## Pinax Testimonials
class TranslatedTestimonial(TranslationOptions):
    fields = ('text', 'author', 'affiliation')

translator.register(Testimonial, TranslatedTestimonial)

My setup Includes:

  • Mezzanine 4.2.2
  • Django 1.10.8
  • Python 2.7.12
  • PostgreSQL 9.5.8
  • Linux 4.10.0-33-generic

Relevant settings.py:

USE_I18N = True
USE_L10N = True

LANGUAGE_CODE = "en"

# Supported languages
LANGUAGES = (
    ('en', _('English')),
    ('es', _('Spanish')),
)

## ModelTranslation Settings
USE_MODELTRANSLATION = True

MODELTRANSLATION_LANGUAGES = ('en', 'es')

## Search Model
SEARCH_MODEL_CHOICES = None

#########
# PATHS #
#########
## Custom Theme Path
THEME_URL = "theme1/"

# Full filesystem path to the project.
PROJECT_APP_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_APP = os.path.basename(PROJECT_APP_PATH)
PROJECT_ROOT = BASE_DIR = os.path.dirname(PROJECT_APP_PATH)

## L10n Path
LOCALE_PATHS = (os.path.join(PROJECT_ROOT + THEME_URL + "/locale/"),)
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, THEME_URL, STATIC_URL.strip("/"))
MEDIA_URL = THEME_URL + STATIC_URL + "media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))

urls.py:

from __future__ import unicode_literals

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.views.i18n import set_language
from mezzanine.core.views import direct_to_template
from mezzanine.conf import settings

admin.autodiscover()

urlpatterns = i18n_patterns(
    url("^admin/", include(admin.site.urls)),
)

if settings.USE_MODELTRANSLATION:
    urlpatterns += [
        url('^i18n/$', set_language, name='set_language'),
    ]

urlpatterns += [
    url("^$", direct_to_template, {"template": "index.html"}, name="home"),
    url("^portfolio/$", direct_to_template, {"template": "portfolio.html"}, name="portfolio"),
    url("^about/$", direct_to_template, {"template": "about.html"}, name="about"),
    url("^services/$", direct_to_template, {"template": "services.html"}, name="services"),
    url("^pricing/$", direct_to_template, {"template": "pricing.html"}, name="pricing"),

    # ``mezzanine.urls``.
    url("^", include("mezzanine.urls")),

]

handler404 = "mezzanine.core.views.page_not_found"
handler500 = "mezzanine.core.views.server_error"

Environment:

beautifulsoup4==4.6.0
bleach==1.5.0
certifi==2017.7.27.1
chardet==3.0.4
Django==1.10.8
django-appconf==1.0.2
django-contrib-comments==1.8.0
django-meta==1.4
django-modeltranslation==0.12
filebrowser-safe==0.4.7
future==0.16.0
grappelli-safe==0.4.7
html5lib==0.9999999
idna==2.6
Mezzanine==4.2.2
oauthlib==2.0.3
olefile==0.44
Pillow==4.2.1
pinax-testimonials==1.0.5
psycopg2==2.7.3.1
pytz==2017.2
requests==2.18.4
requests-oauthlib==0.8.0
six==1.10.0
tzlocal==1.4
urllib3==1.22
webencodings==0.5.1

Please let me know if you need any additional info.

1

There are 1 answers

0
BugZap On

I finally figured it out! And unfortunately it took me a while...

I had given up on the whole thing until I jumped back into the pit of despair and realized I had broke ALL my forms when USE_MODELTRANSLATION = True.

I found out that the <form> tag was not closed out like so >>></form> in my hack-ish templates/includes/language_selector.html. It was then carrying over to my search form that I had placed in the footer in base.html. This was the main reason why I only was encountering the issue when USE_MODELTRANSLATION = True in settings.py.

The lesson learned... DOUBLE-CHECK EVERYTHING BEFORE POSTING!!!

My sincerest apologies to those who attempted to waste time like I did on investigating this only to find nothing wrong at all with django_modeltranslation or mezzanine.

<facepalm />