Django笔记 自定义ListView通用视图

资料

https://ccbv.co.uk/projects/Django/1.4/django.views.generic.list/ListView/
http://stackoverflow.com/questions/22902457/django-listview-customising-queryset

自定义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

你可能感兴趣的:(Django,Django学习笔记)