更改django-haystack中的默认视图
我刚接触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)
我想添加一张图片来更有效地沟通,但因为声望不够,没办法。 :(
1 个回答
1
你可以在urls.py文件中更改表单类和视图类。可以这样做:
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示例,或者看看这个视频 haystack示例视频