0

我一直在尝试使用 django-modeltranslation 让我的搜索功能在我的夹层项目上正常工作。我对 Django、Mezzanine 和 Python 也很陌生,所以我很难解决这个问题也就不足为奇了。

我的翻译工作正常。那里没有问题。但是,每次我USE_MODELTRANSLATION = True在 settings.py 中设置并执行搜索查询时,我每次都会被重定向到我的主页,而不是预期的搜索结果页面,并且在我的控制台输出中我会看到"POST /i18n/ HTTP/1.1" 302 0.

对于后者,如果我设置USE_MODELTRANSLATION = False并执行搜索查询,我会得到预期的搜索结果并在我的输出"GET /search/?q=test HTTP/1.1" 200 12972中。

我还注意到,每个 POST 也在language标题中传递我怀疑是问题的一部分的参数。我还怀疑我的 urls.py 存在一些问题,并尝试了许多特定于搜索 url 的组合,但也没有任何运气。我几乎可以肯定问题可能出在 modeltranslation 上set_language

到目前为止,我已经使用以下组合测试了我的场景,但没有解决:

  1. 夹层4.2.3 | 丹戈 1.11 | django-model翻译 0.12
  2. 夹层4.2.0 | 丹戈 1.10 | django-model翻译 0.12
  3. 夹层4.1.0 | 丹戈 1.10 | django-model翻译 0.11
  4. 夹层4.0.1 | 丹戈 1.9.12 | django-model翻译 0.11
  5. 夹层4.2.2 | 丹戈 1.10.8 | django-modeltranslation 0.12 (目前在这个)

我还在当前设置中包含了以下补丁,因为我确实遇到了同步翻译字段和运行的问题python manage.py createdb

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

如果有人能指出我正确的方向来解决 Mezzanine 和 django- modeltranslation的搜索功能,那将不胜感激!


我的 models.py 和 views.py 是裸露的,因为我现在只是想弄清楚这个问题。反正我在那里没有做任何花哨的事情。

使用我的 translation.py,我仍然需要清理旧的导入,但目前只有我需要翻译 pinax 推荐的字段:

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)

我的设置包括:

  • 夹层 4.2.2
  • Django 1.10.8
  • Python 2.7.12
  • PostgreSQL 9.5.8
  • Linux 4.10.0-33-通用

相关设置.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("/"))

网址.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"

环境:

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

如果您需要任何其他信息,请告诉我。

4

1 回答 1

0

我终于想通了!不幸的是,我花了一段时间......

我已经放弃了整件事,直到我跳回绝望的深渊并意识到我已经打破了所有的形式USE_MODELTRANSLATION = True

我发现标签在我的 hack-ish 中<form>没有像这样 >>> 关闭。然后它会转移到我放置在页脚中的搜索表单中。这是我只在 settings.py中遇到问题的主要原因。</form>templates/includes/language_selector.htmlbase.htmlUSE_MODELTRANSLATION = True

吸取的教训... 发帖前请仔细检查所有内容!!!

我向那些试图像我一样浪费时间进行调查却发现 django_modeltranslation 或 mezzanine 没有任何问题的人致以最诚挚的歉意。

<facepalm />

于 2017-12-02T07:53:38.903 回答