简体   繁体   中英

Can you add context to Django Haystack from urls.py?

I'd like to get the number of search results in the template but Django Haystack's SearchView only provides

context = {
'query': self.query,
'form': self.form,
'page': page,
'paginator': paginator,
'suggestion': None,
}

I noticed SearchView defines

def extra_context(self):
"""
Allows the addition of more context variables as needed.

Must return a dictionary.
"""
return {}

But is there a way to do that in urls.py because I'm happy with what I have there (don't have to do any work in views.py).

This is my urls.py:

urlpatterns += patterns('haystack.views',
    url(r'^search/', 
        login_required(lambda request: SearchView(
        searchqueryset=SearchQuerySet().filter(author=request.user).order_by('-pub_date'),
        form_class=SearchForm
    )(request)), name='haystack_search'),
)

Thanks for any suggestions!

在您的模板中,您应该进行{{ searchqueryset.count }} ,然后不必使您的urls.py更加混乱。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM