2017-09-13 62 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 />