django-haystack:get_absolute_url用于searchresult返回列表对象:['/ test / 2018/05/06 / something_testing /']

时间:2018-05-18 21:19:05

标签: django solr

第一次发布。请原谅我的错误。

我正在尝试使用干草堆工作 django == 2.0.3 来构建知识库网站。我可以看到搜索结果。但是,在我的 href标记的搜索模板中,我可以将get_absolute_url的列表对象看作"['/test/2018/05/06/something_testing/']"。因为这个网址重定向无法正常工作,因为'[]'这些括号介于两者之间。

views.py

@login_required
def techpost_search(request):
    form = SearchForm()
    object_list= Techpost.published.all()
    if request.method == 'GET':
        form = SearchForm(request.GET)
        if form.is_valid():
            cd = form.cleaned_data
            query= request.GET.get('query')
            # results = Techpost.published.filter(Q(title__icontains=query) |           Q(body__icontains=query))
            results = SearchQuerySet().models(Techpost).filter(content=cd['query'])
            total_results = results.count()
            return render(request, 'rincon/search.html', {'form':form,  'results': results, 'total_results': total_results, 'cd': cd})
     return render(request, 'rincon/search.html', {'form':form,})

search_index.py

class TechpostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    publish = indexes.DateTimeField(model_attr='publish')
    title = indexes.CharField(model_attr='title')
    author = indexes.CharField(model_attr='author')
    url = indexes.CharField()


    def get_model(self):    
        return Techpost

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


    def prepare_url(self, obj):
        return (''.join(obj.get_absolute_url()))

search.htmL

{% block content %}

{% if "query" in request.GET %}
<h4>techposts containing "{{ cd.query }}"</h4>
<h5>Found {{ total_results }} result{{ total_results|pluralize}}</h5>

{% for result in results %}

    {% with techpost=result %}
       <a href="{{ techpost.url}}">{{ techpost.title }} </a>
       <p class="date"> Published {{ techpost.publish }} by {{ techpost.author }}</p>
    {% endwith %}

{% empty %}
    <p>There are no results for your query.</p>
    <p><a href="{% url 'rincon:techpost_search' %}">Search again</a></p>
{% endfor %}

{% endif %}

{% endblock %}

完整网址:

https://example.com/rincon/search/['/rincon/2018/05/06/mapi-auto-reply-configuration-oceanfax/']

它应该是: https://example.com/rincon/2018/05/06/mapi-auto-reply-configuration-oceanfax/

设置:

certifi==2018.4.16
chardet==3.0.4
defusedxml==0.5.0
Django==2.0.3
django-allauth==0.36.0
django-crispy-forms==1.7.2
django-haystack==2.8.1
django-taggit==0.22.2
idna==2.6
Markdown==2.6.11
oauthlib==2.0.7
psycopg2==2.7.4
pysolr==3.7.0
python3-openid==3.1.0
pytz==2018.4
requests==2.18.4
requests-oauthlib==0.8.0
urllib3==1.22
solr 6.6.3

1 个答案:

答案 0 :(得分:0)

这听起来好像你还没有实现Haystack的Solr架构。如果你不这样做 - 而你只是在Schemaless模式下运行Solr - 默认情况下所有字段都是多值的。当返回这些字段时,它们将作为JSON数组返回(即['foo','bar']等。)

使用build_solr_schema可以为单个Solr服务器generate the schema.xml file(和solrconfig.xml):

./manage.py build_solr_schema --configure-directory=<CoreConfigDif> --reload-core