0

I have a index.html where my form is implemented:

...
{% block searchform%} {% endblock %}
...

In my search/ folder I have two files.

search.html, which is the actual form for the index.html

{% extends 'index.html' %}

{% block searchform %}
    <form method="get" action="/results/">
        <table>
            {{ form.as_table }}
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Search">
                </td>
            </tr>
        </table>
{% endblock %}

and the file result.html, where I want to see my search results:

{% if query %}
<h3>Results</h3>
...
{% endif %}

urls.py:

from django.conf.urls import include, patterns, url
from dev_devices.views import *

urlpatterns = patterns('',
    url(r'^$', include('haystack.urls')),
    url(r'^results/$', results, name = 'results')
)
view.py

def results(request):
    return render_to_response('results.html')

My problem is, that there are no results showing up. When I click on the submit Button of my form, I will redirected to the correct file (results.html), but no results are showing up. This question exists already but i found no answer, so can someone help me? Thank you !

Alessandro
  • 643
  • 1
  • 7
  • 20

1 Answers1

0

Well, you aren't passing any results to the template. Check out your views.py:

def results(request):
    return render_to_response('results.html')

Your template has this logic:

{% if query %}
<h3>Results</h3>
...
{% endif %}

query is not a variable in context. You'd have to pass the query in from the view:

def results(request):
    return render_to_response('results.html', {'query': 'some query'})

But more than that, you need to get the actual results from Haystack and put those in context too. I think you are confused about what's happening here. If you want to use haystacks default views for search/results, then you shouldn't define any custom views at all. The way your results view is defined, how is Django to know this page has anything to do with Haystack at all?

If you do want to define custom views, then those views need to implement Haystack search forms, creation of SearchQuerySets, pagination, (and so on), and pass that data to the template. See the Haystack documentation on creating you own views:

http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#creating-your-own-view

If you just want to see your results and this particular URL structure is not important to you, then just change your form action to point at the same page action="" and move your results display logic from results.html to search.html. That should get you the basic search results that ship with Haystacks default views and forms.

Casey Kinsey
  • 1,451
  • 9
  • 16