2013-10-20 69 views
0

我遵循了入门文档和所有工作! :)自定义输入字段和自定义searchqueryset

但我想用自定义表单替换/search/search.html中的表单而不选择模型复选框。

在形式我想补充一个按钮,点击后,由标准的搜索结果排序 我的问题是:

哪些文件,我需要创建或修改,以同名但认为和他们有什么作用?

我的代码是:

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.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:我的工作在Django项目机智h 1.5.1版和whoosh喜欢干草堆后端。

感谢您的帮助:)

回答

0

你所得到的可选模型复选框,因为你延长ModelSearchForm:

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

您可以轻松地通过扩展简单的搜索变种避免此行为,

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

然后你要添加的东西像下面这样你的应用程序的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'), 
) 

请注意,使用.models()过滤器时,会出现与最新版本的Haystack和Whoosh一起使用时的错误。如果您在上述策略中遇到任何问题,那么您应该确保您的Haystack和Whoosh的版本2.0.0和2.4.1经过相对测试并且运行良好。

此外,与您的问题无关,您可能希望避免在settings.py中使用HAYSTACK_SEARCH_RESULTS_PER_PAGE。它也有一个非常丑陋的错误。只是分享我的经验。请注意,此信息与Whoosh有关,所有其他后端应该都可以正常工作。

+0

谢谢@AlexParakhnevich。有用! – angebouabre