2013-05-09 64 views
1

我有一個視圖,放置一些上下文,並從這個上下文呈現模板。如果沒有搜索查詢,我希望查看顯示的所有內容,並顯示搜索到的內容,如果搜索到任何內容。django haystack - 如何使用當前視圖有一些上下文

class MyCurrentView(View): 
    def get(self, request): 
     data = { 'users': User.objects.all() } 
     return render_to_response("mytemp.html", .. 

urls.py: 
     url(r'^search/$', MyCurrentView.as_view()) 

現在,我與搜索查看這樣的集成這樣的:如果有必要

class MyCurrentView(SearchView): (If u observe, I subclassed SEarchView). 
    template = 'mytemp.html' 
    def get(self, request): 
     data = { 'users': User.objects.all() } 
     return render_to_response... 

    def get_context_data(self, ...): 
      print "....this should print" #But not printing. So, unable to add data 
    return data 

urls.py: 
     url(r'^search/$', MyCurrentView.as_view()) 
     # as_view() gave me error, so I did MyCurrentView() , may be thats y, get_context_data not calling. 

將提供更多的信息。

+0

得到了這個答案,只是重寫extra_context定義。但是,誰能告訴我在urls.py中調用VIEwname.as_view()和ViewName()之間的區別? – user2139745 2013-05-09 16:08:08

+0

Haystack的SearchViews不像Django的常規基於類的視圖。他們不使用'as_view'模式。 – 2013-05-10 13:37:52

+0

Haystack現在通過PR#1130擁有傳統的django CBV。看到我的評論下面或https://github.com/toastdriven/django-haystack/pull/1130 – 2015-01-14 15:18:00

回答

0

新的Django樣式類通過PR#1130加爲本次到Django的乾草堆:

您現在可以使用搜索視圖就像你通常會使用Django(見變化在拉請求中)。

from haystack.generic_views import SearchView 

class MySearchView(SearchView): 

    def get_context_data(self, **kwargs): 
     # do whatever you want to context 
相關問題