gpt4 book ai didi

django - (haystack + whoosh) {{ result.object.get_absolute_url }} 不起作用

转载 作者:行者123 更新时间:2023-12-02 07:09:33 25 4
gpt4 key购买 nike

我在我的 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)。在 URL 中:(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 - (haystack + whoosh) {{ result.object.get_absolute_url }} 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27968892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com