I am using django-haystack and I am trying to implement a way to append the page number to a pdf link in order to open it in the specific page. My goal is to open the pdf in the page where the first hit is found. I know the position of the hit in my document and the position where the page changes. For example i know that the first hit starts at character 2067 and the second page changes at character 3000, so i have to open the pdf at the second page.
My question is: how can i get the result of the function that finds the number of the page where the pdf should open and render it ?
I am thinking that the result should be something like that <a href="{% static 'img/sample.pdf#page={{ pageNumber }}' %}">
but i am open to any other suggestions.
P.S. I am not asking you to solve my problem. I am asking for suggestions or a constructive discussion as i am new to django.
Thank you in advance
EDIT
So after researching for a bit i did the following. I found that the highlighter class has a function that finds the position of the hits. I added a getter to that class in order to get the positions (I will change it later. For now i want to see if it is working the way i think it should). Then in my views.py
file i added the following
from django.shortcuts import render
from haystack.utils.highlighting import Highlighter
def getPage(request):
pos = Highlighter.getPos()
print (pos)
return render(request, 'search/_result_object.html', {'pos': pos})
and in my html i added this
<ul>
{% for element in pos %}
<li>{{ element }}</li>
{% endfor %}
</ul>
just to print the position and see that everything works ok. But the list is empty, meaning that i get no results. Maybe something is not working the way i think it should. Any ideas?
Edit #2
I think that it is impossible to get the positions from the highlighter since I don't have the actual Highlighter object in order to get the positions using a getter.
Is there any other way to pass arguments between highlighter and a view? I managed to get the query term in my view but i don't have the text block where the query term was found nor the full text to search again for the position. In addition, i think that this approach would be slow when the program scales up.
print(pos)
in your view prints nothing? – Secernpos = Highlighter.getPos()
. Is that the exact code you are using? I would expect that to crash sinceHighlighter
doesn't have an attributegetPos
. So show us the code you use to getpos
. – SecernHighlighter
class. Then please show us your code of getting that instance/object. Maybe you should first go through some examples. – Gibbsite