2017-09-23 53 views
1

我得到了以下錯誤,當我試圖建立Solr模式:Django的草堆 - 無法建立Solr模式

(my_env) [email protected] ~/Documents/Django/mysite $ python manage.py build_solr_schema 
Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line 
    utility.execute() 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 356, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/core/management/base.py", line 330, in execute 
    output = self.handle(*args, **options) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/haystack/management/commands/build_solr_schema.py", line 29, in handle 
    schema_xml = self.build_template(using=using) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/haystack/management/commands/build_solr_schema.py", line 57, in build_template 
    return t.render(c) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/template/backends/django.py", line 64, in render 
    context = make_context(context, request, autoescape=self.backend.engine.autoescape) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/template/context.py", line 287, in make_context 
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__) 
TypeError: context must be a dict rather than Context. 

也許這些信息將是有益的:

的mysite/settings.py文件:

""" 
Django settings for mysite project. 

Generated by 'django-admin startproject' using Django 1.11.5. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.11/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.11/ref/settings/ 
""" 

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '****' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 

SITE_ID = 1 

# Application definition 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.sites', 
    'django.contrib.sitemaps', 
    'blog', 
    'taggit', 
    'haystack', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'mysite.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 = 'mysite.wsgi.application' 


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

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


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

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


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

STATIC_URL = '/static/' 

HAYSTACK_CONNECTIONS = { 
    'default': { 
     'ENGINE': 'haystack.backends.solr_backend.SolrEngine', 
     'URL': 'http://127.0.0.1:8983/solr/blog' 
    }, 
} 

博客/ search_indexes.py文件:

from haystack import indexes 
from .models import Post 

class PostIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    publish = indexes.DateTimeField(model_attr='publish') 

    def get_model(self): 
     return Post 

    def index_queryset(self, using=None): 
     return self.get_model().published.all() 

博客/模板/搜索/索引/博客/ post_text.txt文件:

{{ object.title }} 
{{ object.tags.all|join:", " }} 
{{ object.body }} 

我使用Apache Solr實現4.10.4,Python的3.4.5和Django的1.11.5。當我試圖在Python控制檯我得到以下錯誤導入乾草堆:

>>> import haystack 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/haystack/__init__.py", line 10, in <module> 
    from haystack.constants import DEFAULT_ALIAS 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/haystack/constants.py", line 10, in <module> 
    ID = getattr(settings, 'HAYSTACK_ID_FIELD', 'id') 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/conf/__init__.py", line 56, in __getattr__ 
    self._setup(name) 
    File "/home/pecan/Documents/Django/my_env/lib/python3.4/site-packages/django/conf/__init__.py", line 39, in _setup 
    % (desc, ENVIRONMENT_VARIABLE)) 
django.core.exceptions.ImproperlyConfigured: Requested setting HAYSTACK_ID_FIELD, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.in Python console 

我在數尋求幫助。

回答

1

草堆在我的Django項目的版本是錯誤的。我使用了django-haystack 2.6.1包,但它在傳遞django模板上下文時遇到了問題。該版本在上下文中傳遞了一個Context對象而不是字典。更多細節:https://github.com/django-haystack/django-haystack/pull/1504/commits/295584314e19a191a59450e053b21809adceca2a

草堆/管理/命令/ build_solr_schema.pydjango-haystack 2.6.1

 content_field_name, fields = backend.build_schema(
     connections[using].get_unified_index().all_searchfields() 
    ) 
    return Context({ 
     'content_field_name': content_field_name, 
     'fields': fields, 
     'default_operator': constants.DEFAULT_OPERATOR, 
     'ID': constants.ID, 
     'DJANGO_CT': constants.DJANGO_CT, 
     'DJANGO_ID': constants.DJANGO_ID, 
    }) 

def build_template(self, using): 
    t = loader.get_template('search_configuration/solr.xml') 

草堆/管理/命令/ build_solr_schema.pydjango-haystack 2.7.dev0

 content_field_name, fields = backend.build_schema(
     connections[using].get_unified_index().all_searchfields() 
    ) 

    return { 
     'content_field_name': content_field_name, 
     'fields': fields, 
     'default_operator': constants.DEFAULT_OPERATOR, 
     'ID': constants.ID, 
     'DJANGO_CT': constants.DJANGO_CT, 
     'DJANGO_ID': constants.DJANGO_ID, 
    } 

def build_template(self, using): 
    t = loader.get_template('search_configuration/solr.xml') 

我不得不卸載django-haystack 2.6.1並使用命令安裝較新的版本:

pip uninstall django-haystack 
pip install django-haystack==2.7.dev0 

我也解決了導入錯誤。在這種情況下,我只是將行HAYSTACK_ID_FIELD = 1添加到設置。py文件,我將所需的環境變量DJANGO_SETTINGS_MODULE設置爲值mysite.settings

我編輯settings.py後執行以下命令:

(my_env) [email protected] ~/Documents/Django/mysite $ DJANGO_SETTINGS_MODULE="mysite.settings" 
(my_env) [email protected] ~/Documents/Django/mysite $ echo $DJANGO_SETTINGS_MODULE 
mysite.settings 
(my_env) [email protected] ~/Documents/Django/mysite $ DJANGO_SETTINGS_MODULE python 
bash: DJANGO_SETTINGS_MODULE: command not found 
(my_env) [email protected] ~/Documents/Django/mysite $ python 
Python 3.4.5 (default, Sep 17 2017, 18:19:56) 
[GCC 5.4.0] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import haystack 
>>> 

現在django-haystack是否正常工作!

0

調試草堆

有一些常見問題的人使用運行草堆在第一次進入時。

「無模塊命名大海撈針。」通常先加入草堆到您的項目時,會發生

此問題。

  • 是否使用您的django-haystack 結帳內haystack目錄/安裝?

  • 是您的PYTHONPATH上的haystack目錄?或者,將 haystack符號鏈接到您的項目中?

  • 啓動Django的外殼(./manage.py shell),並嘗試import haystack。您 可能會收到一個不同的,更具描述性的錯誤消息。

  • 仔細檢查以確保您沒有循環進口。 (A即模塊 嘗試從模塊B正試圖從模塊導入 A.進口)

「沒有找到結果。」(在網頁)

幾個問題可以導致無法找到結果。大多數情況下,它不是運行rebuild_index來填充索引或有空白document=True字段,導致沒有內容供引擎搜索。

  • 你有位於安裝的應用程序內的search_indexes.py

  • 你的數據庫中有數據嗎?

  • 您是否運行./manage.py rebuild_index來索引您的所有 內容?

  • 嘗試運行./manage.py rebuild_index -v2以獲取更詳細的輸出到 確保正在處理/插入數據。

  • 啓動Django的外殼(./manage.py shell),並嘗試:

from haystack.query import SearchQuerySet

sqs = SearchQuerySet().all()

sqs.count()

  • 應該會得到一個整數> 0。如果沒有,檢查以上和 REINDEX。

sqs[0] # Should get back a SearchResult object.

sqs[0].id # Should get something back like 'myapp.mymodel.1'.

sqs[0].text # ... or whatever your document=True field is.

  • 如果你獲得了要麼u''None,這意味着你的數據不是 使其成被搜索的主要領域。您需要檢查 該字段是否具有使用模型數據的模板,該模板直接從模型中提取數據或者在索引時填充數據的方法。
  • 檢查您的搜索頁面的模板,並確保它正在循環通過 正確的結果。此外,請確保它正在訪問從搜索引擎返回的有效 字段,或者正在嘗試通過{{ result.object.foo }}查找來嘗試訪問關聯模型 。

來源:http://django-haystack.readthedocs.io/en/v2.4.1/toc.html

+0

對不起,但這個答案沒有幫助。我描述了我是如何在自己的答案中解決這個問題的。 –