I am following tutorials that use haystack, and their view templates are set up like this
{% for result in results %}
{% with post=result.object %}
<h4><a href="{{ post.get_absolute_url }}">{{ result.title }}</a></h4>
{{ post.body|truncatewords:5 }}
{% endwith %}
{% empty %}
<p>There are no results for your query.</p>
{% endfor %}
This is the part that interests me
{% with post=result.object %}
and this
<h4><a href="{{ post.get_absolute_url }}">{{ result.title }}</a></h4>
typically from what I've read the syntax is
result.object.get_absolute_url
but he obviously did this
with post=result.object
to make his work without object
My thing is this; I don't have access to get_absolute_url. I don't have the actual object returned. Only pieces of it that are obtained using this
publish = indexes.DateTimeField(model_attr='publish')
title = indexes.CharField(model_attr='title')
body = indexes.CharField(model_attr='body')
Now with those, I can do this:
{% for result in results %}
{% with post=result.object %}
{{result.publish}}<br>
{{result.title}}<br>
{{result.body}}<br><hr>
#......
but this is not the actual object just stripped off pieces how can I get the actual object so I can use
href="{{result.object.get_absolute_url}}"
instead of having to do this
slug = indexes.CharField(model_attr='slug')
then having to do this
href="{% url 'blog:post_detail' result.slug %}
You do have the object, in result.object
. You've used a with
statement to assign that to post
, but then you've ignored that variable completely.