(haystack + whoosh) {{ result.object.get_absolute_url }} is not working

旧城冷巷雨未停 提交于 2019-12-11 03:22:31

问题


I am using haystack (2.1.1) and whoosh in my django (1.7) website. i am happy because it is working, but not completely. the app show the right searches but when i click in the results it doesn't go to the product page. it looks like i haven't configured something that make {{ result.object.get_absolute_url }} doesnt work properly. I hope any of you can help me (as reference i am putting all the code)

this is my app models (products/models)

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')

this is my search_indexes.py, that i put in the same folder of my app (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())

I did the changes in the settings file

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

create the file in my template folder "templates/search/indexes/products/products_text.txt"

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

the HTML and urls are the same as in the website of haystack (just change the result.object.title for result.object.name). in URLS: (r'^search/', include('haystack.urls')) and 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 %}

as i said before it does the search and show it. but i don't know why the {{ result.object.get_absolute_url }} is not working, so it shows the product tittle but doesn't link them to their pages.


回答1:


You just need to define a get_absolute_url method explicitly on your model class:

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

It would be even better to use reverse within this method, which will depend on your urlconf. More details here.



来源:https://stackoverflow.com/questions/27968892/haystack-whoosh-result-object-get-absolute-url-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!