我正在django(1.7)网站中使用haystack(2.1.1)和whoosh。我很高兴,因为它正在工作,但并不完全。该应用程序显示正确的搜索,但是当我单击搜索结果时,它不会转到产品页面。看来我尚未配置使{{result.object.get_absolute_url}}无法正常运行的功能。我希望任何人都可以帮助我(作为参考,我将所有代码都放入)

这是我的应用程序模型(产品/模型)

from django.db import models

class Products(models.Model):
   name = models.CharField(max_length=120)
   description = models.TextField()
   image1 = models.ImageField(upload_to='product_images', blank=True, null=True)
   price = models.FloatField(default=0.00)
   slug = models.CharField(max_length=50, blank=False, null=True)
   pub_date = models.DateTimeField()

   def __unicode__(self):
       return str(self.name)


   class Meta:
      ordering =['-id']
      verbose_name = ('Product')
      verbose_name_plural = ('Products')


这是我的search_indexes.py,我将其放在应用程序的同一文件夹中(products / search_indexes.py)

import datetime
from haystack import indexes
from products.models import Products


class ProductsIndex(indexes.SearchIndex, indexes.Indexable):
  text = indexes.CharField(document=True, use_template=True)
  name = indexes.CharField(model_attr='name')
  description = indexes.CharField(model_attr='description')
  pub_date = indexes.DateTimeField(model_attr='pub_date')

  def get_model(self):
     return Products

  def index_queryset(self, using=None):

    return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())


我在设置文件中进行了更改

 HAYSTACK_CONNECTIONS = {
'default': {
    'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
    'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
   },
 }


在我的模板文件夹“ templates / search / indexes / products / products_text.txt”中创建文件

 {{ object.name }}
 {{ object.description }}


HTML和url与haystack网站中的相同(只需将result.object.title更改为result.object.name)。在网址中:(r'^ search /',include('haystack.urls'))和html(templates / search / search.html)

 {% extends 'base.html' %}

 {% block content %}
   <h2>Search</h2>

   <form method="get" action=".">
     <table>
        {{ form.as_table }}
         <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Search">
            </td>
         </tr>
     </table>

    {% if query %}
        <h3>Results</h3>

        {% for result in page.object_list %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.name }}</a>
            </p>
        {% empty %}
            <p>No results found.</p>
        {% endfor %}

        {% if page.has_previous or page.has_next %}
            <div>
                {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}

                {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
            </div>
        {% endif %}
    {% else %}
        {# Show some example queries to run, maybe query syntax, something else? #}
    {% endif %}
 </form>
{% endblock %}


正如我之前所说,它进行搜索并显示出来。但我不知道为什么{{result.object.get_absolute_url}}无法正常工作,因此它显示了产品标题,但没有将其链接到他们的页面。

最佳答案

您只需要在模型类上显式定义一个get_absolute_url方法:

class Products(models.Model):
    ...
    def get_absolute_url(self):
        return "/products/%s/" % self.slug


最好在此方法中使用reverse,这取决于您的urlconf。更多详细信息here

关于django - (干草堆+飞快移动){{result.object.get_absolute_url}}无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27968892/

10-14 18:00
查看更多