2017-09-13 66 views
0

我一直在努力让我的搜索功能在使用django-modeltranslation的夹层项目上正常工作。我对Django,Mezzanine和Python相当陌生,因此我不明白为什么我很难解决这个问题。夹层4.2.2/4.2.3:USE_MODELTRANSLATION = True时的搜索中断

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

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

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

到目前为止,我测试了我的方案与以下的组合,但没有解决:

  1. 夹层4.2.3 | Dango 1.11 | django-modeltranslation 0.12
  2. 夹层4.2.0 | Dango 1.10 | django-modeltranslation 0.12
  3. 夹层4.1.0 | Dango 1.10 | django-modeltranslation 0.11
  4. 夹层4.0.1 | Dango 1.9.12 | django-modeltranslation 0.11
  5. 夹层4.2.2 | Dango 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

如果有人能指出我正确的方向来解决与夹层和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) 

我的设置包括:

  • 夹层4.2.2
  • Django 1.10.8
  • Python 2.7。12
  • 的PostgreSQL 9.5.8
  • Linux的4.10.0-33泛型

相关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 

如果您需要任何其他信息,请让我知道。

回答

0

我终于明白了!而不幸的是,我花了我已经放弃了整个事情,直到我跳回到了绝望的坑,并意识到我必须打破了所有我的表单时USE_MODELTRANSLATION = True一段时间...

我发现<form>标记没有被封闭,像这样>>></form>在我的hack-ish templates/includes/language_selector.html中。然后,我把它放在页脚base.html的搜索表单中。这是我在settings.py中只遇到USE_MODELTRANSLATION = True问题的主要原因。

吸取的教训... 双重检查一切之前,张贴!

我最诚挚的歉意那些谁试图把时间浪费在像我一样在调查这个才发现没有什么错处均采用django_modeltranslation或夹层。

<facepalm />