首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >夹层4.2.2 / 4.2.3:当USE_MODELTRANSLATION = True时搜索中断

夹层4.2.2 / 4.2.3:当USE_MODELTRANSLATION = True时搜索中断
EN

Stack Overflow用户
提问于 2017-09-13 02:09:18
回答 1查看 138关注 0票数 0

我已经有相当长的时间试图让我的搜索功能在我的夹层项目上使用django-模型翻译正确地工作。我对Django、Mezzanine和Python也相当陌生,所以我不奇怪为什么我很难解决这个问题。

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

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

我还注意到,每个帖子也在头中传递language参数,我怀疑这是问题的一部分。我还怀疑我的urls.py存在一些问题,并尝试了许多特定于搜索url的组合,但也没有任何进展。我几乎可以肯定这个问题可能与模型翻译set_language有关。

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

  1. 夹层4.2.3 \ Dango 1.11 x django-模型翻译0.12
  2. 夹层4.2.0 \ Dango 1.10 x django-模型翻译0.12
  3. 夹层4.1.0 \ Dango 1.10 \django-模型翻译0.11
  4. 夹层4.0.1 Dango 1.9.12 x django-模型翻译0.11
  5. 夹层4.2.2 \ Dango 1.10.8 \Dango模型转换0.12 (目前在此)

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

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

如果有人能为我指出正确的方向来解决搜索功能与夹层和django-模型翻译,这将是,非常非常感谢!

我的models.py和views.py是赤裸裸的,因为我目前正试图解决这个问题。反正我在里面也没做什么花哨的事。

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

代码语言:javascript
复制
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-泛型

相关settings.py:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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"

环境:

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

发布于 2017-12-02 07:53:38

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

我已经放弃了所有的事情,直到我跳回绝望的坑,并意识到我已经打破了我的所有形式时,USE_MODELTRANSLATION = True

我发现<form>标签在我的hack-ish templates/includes/language_selector.html中并没有像>>></form>那样被关闭。然后,它带着我放在base.html页脚中的搜索表格。这就是为什么我在USE_MODELTRANSLATION = True in settings.py时才遇到这个问题的主要原因。

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

我向那些试图浪费时间的人表示最诚挚的歉意,就像我在调查这件事时一样,结果发现django_modeltranslation或夹层一点问题都没有。

<facepalm />

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46187772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档