我已经有相当长的时间试图让我的搜索功能在我的夹层项目上使用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有关。
到目前为止,我已经用以下组合测试了我的场景,但没有解决方案:
我还在当前的设置中包含了以下修补程序,因为我遇到了问题,同步翻译字段和运行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推荐的字段:
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)我的设置包括:
相关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"环境:
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,如果您需要任何其他信息,请告诉我.
发布于 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 />
https://stackoverflow.com/questions/46187772
复制相似问题