(干草堆+飞快移动){{result.object.get_absolute_url}}无法正常工作 [英] (haystack + whoosh) {{ result.object.get_absolute_url }} is not working

查看:132
本文介绍了(干草堆+飞快移动){{result.object.get_absolute_url}}无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

create the file in my template folder "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)

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 %}

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

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.

推荐答案

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

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

使用 reverse甚至会更好在此方法中,具体取决于您的urlconf。 此处

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

这篇关于(干草堆+飞快移动){{result.object.get_absolute_url}}无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆