我是 django 和 haystack 的新手。我遵循了 haystack 网站上提供的示例教程,并弄清楚了如何执行基本搜索。我能够获得输出值,但页面显示不正确。 Haystack 默认表单是 searchForm 但在我的例子中默认是 ModelSearchForm。我不知道为什么以及如何改变它。
这是我的 search.html 页面中的表单。
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search" class="btn btn-default">
</td>
</tr>
</table>
</form>
在我的搜索页面中,haystack 总是显示默认表单。它有预定义的标签,它总是显示带有复选框的型号名称。我无法弄清楚它显示这些值的位置以及如何修改它。
我的search_index.py 文件
import datetime
from haystack import indexes
from signups.models import SignUp
class SignUpIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
first_name = indexes.CharField(model_attr='first_name')
last_name = indexes.CharField(model_attr='last_name')
email = indexes.CharField(model_attr='email')
def get_model(self):
return SignUp
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
我的model.py文件
from django.db import models
from django.utils.encoding import smart_unicode
class SignUp(models.Model):
first_name = models.CharField(max_length = 120,null = True, blank= True)
last_name = models.CharField(max_length = 120,null = True, blank= True)
email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
update = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return smart_unicode(self.email)
我希望添加一张图片以更有效地进行交流,但由于声誉较低而无法做到。 :(
请您参考如下方法:
您可以在 urls.py 中更改表单类和 View 类。 像这样的东西:
from haystack.views import SearchView, search_view_factory
from haystack.forms import HighlightedModelSearchForm
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mamotwo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^search/', include('haystack.urls')),
url(r'^mysearch/$', search_view_factory(view_class=SearchView, form_class=HighlightedModelSearchForm), name='mysearch'),
url(r'^admin/', include(admin.site.urls)),
)
但我也是 haystack 的新手。最好检查此代码 haystack demo或者这个视频 haystack demo video
