Вопрос

I followed the get started doc and everyting work well! :)

But i would like to replace the form in the /search/search.html by a custom form without selectable model checkbox.

In the form i would like to add a button which on click, order search results by a criteria My questions are:

Which files i need to create or modified to perfoms that and what are their roles?

My codes are:

search_indexes.py

from haystack import indexes
from models import ProduitCommerce

class ProduitIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    commerce = indexes.CharField(model_attr = 'nom_commerce')
    nom = indexes.CharField(model_attr = 'nom_produit')
    price = indexes.DecimalField(model_attr = 'prix') #Field to filter ON

    def get_model(self):
        return ProduitCommerce   

search/search.html

{% extends 'base.html' %}

{% block content %}
<h2>Search</h2>

<form method="get" action=".">
    <table>
        {{ form.as_table }} <!------ FORM TO CHANGE BY A CUSTOM FORM BLOCK TO INCLUDE WITH DJANGO ------->
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Search">
            </td>
        </tr>
    </table>

    {% if query %}
        <h3>Results</h3>
        <!---------------------------- PLACE TO INCLUDE THE BUTTON TO FILTER ON result.object.prix------------------>
        {% for result in page.object_list %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.nom_produit }}{{result.object.prix}}</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>

produitcommerce_text.txt

{{ object.nom_produit }}
{{ object.nom_commerce }}
{{ object.description }} 
{{ object.prix }} 

PS: I'm working on a django project with the 1.5 .1 version and whoosh like haystack backend.

Thanks for you help :)

Это было полезно?

Решение

You are getting the selectable model checkboxes because you're extending the ModelSearchForm:

#forms.py
from haystack.forms import ModelSearchForm
class ProduitForm(ModelSearchForm):

You can easily avoid this behaviour by extending the simple search form variant, provided by the haystack:

#forms.py
from haystack.forms import SearchForm
class ProduitForm(SearchForm):

Then you're going to add something like the following to you application's urls.py:

#urls.py
from haystack.query import SearchQuerySet
from haystack.views import SearchView, search_view_factory
from myapp.models import Produit

urlpatterns = patterns('',  
    url(r'^produit/search/$', search_view_factory(
        view_class=SearchView,
        template='search/search.html',
        searchqueryset=SearchQuerySet().models(Resume),
        form_class=ProduitForm
    ), name='produit_search'),
)

Please note that there is a bug with .models() filter when trying to use it with latest versions of Haystack and Whoosh. If you would experience any kind of problems with the strategy above, then you should make sure, that your Haystack and Whoosh are of versions 2.0.0 and 2.4.1 relatively - tested and works well.

Also, not related to your question, you might want to avoid using HAYSTACK_SEARCH_RESULTS_PER_PAGE in your settings.py. It also has a very ugly bug. Just sharing my experience. Note that this info is related to Whoosh, everything should work fine with any other backend.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top