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.
將提供更多的信息。
得到了這個答案,只是重寫extra_context定義。但是,誰能告訴我在urls.py中調用VIEwname.as_view()和ViewName()之間的區別? – user2139745 2013-05-09 16:08:08
Haystack的SearchViews不像Django的常規基於類的視圖。他們不使用'as_view'模式。 – 2013-05-10 13:37:52
Haystack現在通過PR#1130擁有傳統的django CBV。看到我的評論下面或https://github.com/toastdriven/django-haystack/pull/1130 – 2015-01-14 15:18:00