2015-07-03 94 views
2

我想在Django 1.8.2上使用Haystack與Elasticsearch 1.6作爲後端,但無法獲得乾草堆教程爲我工作。Django草垛與Elasticsearch後端

在運行rebuild_index我碰到下面的錯誤和堆棧跟蹤

./manage.py rebuild_index 

WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'. 
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command. 
Are you sure you wish to continue? [y/N] Y 
Removing all documents from your index because you said so. 
All documents removed. 
Indexing 388 regestes 
GET /haystack/_mapping [status:404 request:0.002s] 
ERROR:root:Error updating regeste using default 
Traceback (most recent call last): 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 188, in handle_label 
    self.update_backend(label, using) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 233, in update_backend 
    do_update(backend, index, qs, start, end, total, verbosity=self.verbosity, commit=self.commit) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 96, in do_update 
    backend.update(index, current_qs, commit=commit) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/backends/elasticsearch_backend.py", line 166, in update 
    prepped_data = index.full_prepare(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 212, in full_prepare 
    self.prepared_data = self.prepare(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 203, in prepare 
    self.prepared_data[field.index_fieldname] = field.prepare(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 159, in prepare 
    return self.convert(super(CharField, self).prepare(obj)) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 79, in prepare 
    return self.prepare_template(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 136, in prepare_template 
    t = loader.select_template(template_names) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/template/loader.py", line 76, in select_template 
    raise TemplateDoesNotExist(', '.join(template_name_list)) 
django.template.base.TemplateDoesNotExist: search/indexes/regeste/regeste_text.txt 
Traceback (most recent call last): 
    File "./manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line 
    utility.execute() 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute 
    output = self.handle(*args, **options) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/rebuild_index.py", line 26, in handle 
    call_command('update_index', **options) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/__init__.py", line 120, in call_command 
    return command.execute(*args, **defaults) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute 
    output = self.handle(*args, **options) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 183, in handle 
    return super(Command, self).handle(*items, **options) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 619, in handle 
    label_output = self.handle_label(label, **options) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 188, in handle_label 
    self.update_backend(label, using) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 233, in update_backend 
    do_update(backend, index, qs, start, end, total, verbosity=self.verbosity, commit=self.commit) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 96, in do_update 
    backend.update(index, current_qs, commit=commit) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/backends/elasticsearch_backend.py", line 166, in update 
    prepped_data = index.full_prepare(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 212, in full_prepare 
    self.prepared_data = self.prepare(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 203, in prepare 
    self.prepared_data[field.index_fieldname] = field.prepare(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 159, in prepare 
    return self.convert(super(CharField, self).prepare(obj)) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 79, in prepare 
    return self.prepare_template(obj) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 136, in prepare_template 
    t = loader.select_template(template_names) 
    File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/template/loader.py", line 76, in select_template 
    raise TemplateDoesNotExist(', '.join(template_name_list)) 
django.template.base.TemplateDoesNotExist: search/indexes/regeste/regeste_text.txt 

項目的根是這樣的:

. 
    ├── config 
    ├── lib 
    ├── manage.py 
    ... 
    ├── regeste 
    ├── requirements.txt 
    ├── search 
    ├── templates <-- search/indexes/regeste/regeste_text.txt lies here 
    └── untitled 

regeste_txt.txt

{{ object.title }} 
{{ object.abstract }} 

settings.py外觀像這樣(刪除了幾個私密部分)

import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 



INSTALLED_APPS = (
     'elasticsearch', 
     'haystack', 
     'regeste', 
     ) 

MIDDLEWARE_CLASSES = (
     'django.contrib.sessions.middleware.SessionMiddleware', 
     'django.middleware.common.CommonMiddleware', 
     'django.middleware.csrf.CsrfViewMiddleware', 
     'django.contrib.auth.middleware.AuthenticationMiddleware', 
     'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
     'django.contrib.messages.middleware.MessageMiddleware', 
     'django.middleware.clickjacking.XFrameOptionsMiddleware', 
     'django.middleware.security.SecurityMiddleware', 
     ) 

ROOT_URLCONF = 'imperii_viz.urls' 

TEMPLATES = [ 
     { 
      'BACKEND': 'django.template.backends.django.DjangoTemplates', 
      'DIRS': [], 
      'APP_DIRS': True, 
      'OPTIONS': { 
       'context_processors': [ 
        'django.template.context_processors.debug', 
        'django.template.context_processors.request', 
        'django.contrib.auth.context_processors.auth', 
        'django.contrib.messages.context_processors.messages', 
        ], 
       }, 
      }, 
     ] 

WSGI_APPLICATION = 'imperii_viz.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 

DATABASES = { 
     'default': { 
      'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
      'NAME': 'imperii-viz',      # Or path to database file if using sqlite3. 
      # The following settings are not used with sqlite3: 
      'USER': 'postgres', 
      'PASSWORD': 'postgres', 
      'HOST': 'localhost',      # Empty for localhost through domain sockets or   '127.0.0.1' for localhost through TCP. 
      'PORT': '',      # Set to empty string for default. 
      }, 
     'search': { 
      'BACKEND': 'django_redis.cache.RedisCache', 
      'LOCATION': 'redis://127.0.0.1:6379/1', 
      'OPTIONS': { 
       'CLIENT_CLASS': 'django_redis.client.DefaultClient', 
      } 
     } 
     } 


HAYSTACK_CONNECTIONS = { 
    'default': { 
     'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', 
     'URL': 'http://127.0.0.1:9200/', 
     'INDEX_NAME': 'haystack', 
    }, 
} 

# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


#celery settings 

BROKER_URL = 'redis://localhost:6379/0' 

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 

STATIC_URL = '/static/' 
SHELL_PLUS = "ipython" 
TEMPLATE_DIRS = (
     os.path.join(BASE_DIR, 'templates'), 
     ) 

我很感激任何提示問題出在哪裏。

感謝, 丹尼爾

+0

您是否創建了模板「search/indexes/regeste/regeste_text.txt」? –

+0

是的,路徑設置爲./templates/(即在與各個應用程序相同的級別),我已經設置了'TEMPLATE_DIRS =( os.path.join(BASE_DIR,'模板'), )在settings.py ...所以它應該被發現,對吧? – danba

+0

考慮發佈您的應用程序目錄樹和相關設置。你可能做得很好,或者你沒有做到。沒有看到就很難說。 –

回答

2

DIRSTEMPLATES字典優先於TEMPLATE_DIRS, which is deprecated as of Django 1.8

設置DIRS[os.path.join(BASE_DIR, 'templates')]


更好

的是,你應該把在一個應用程序的模板目錄此模板(使用在模型定義的應用程序),因爲這可能是它應該活。

+0

按照您的建議將模板移動到應用的模板目錄中。謝謝。 – danba

0

只需創建模板目錄裏面的應用程序,然後移動搜索目錄到這個模板目錄。現在它會工作。