Django筆記 自定義ListView通用檢視
阿新 • • 發佈:2019-02-13
資料
自定義get_queryset函式
class Browse(generic.ListView):
template_name = 'app/browse.html'
paginate_by = 25
# Modifying the get_context_data method
def get_context_data(self, **kwargs):
context = super(Browse, self).get_context_data(**kwargs)
q = self.request.GET.get("browse" )
context['input'] = q
return context
def get_queryset(self):
queryset = Cats.objects.all()
if self.request.GET.get("browse"):
selection = self.request.GET.get("browse")
if selection == "Cats":
queryset = Cats.objects.all()
elif selection == "Dogs":
queryset = Dogs.objects.all()
elif selection == "Worms":
queryset = Worms.objects.all()
else:
queryset = Cats.objects.all()
return queryset
自定義get和get_context_data函式
def get(self, request, *args, **kwargs) :
#query_string = request.META.get('QUERY_STRING', '')
self.user = request.user
#if self.request.GET.get("Condition"):
return super(thisview, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(thisview, self).get_context_data(**kwargs)
context.update({
'par': self.par,
})
return context