获取Self.Kwargs中不可用的变量

Gre*_*reg 0 python django django-views

我有以下代码.我试图在url中传递一个名为'sort'和'sch'的get变量,但是当我打印self.kwargs时,它返回null.如果有人能指出我正确的方向,那将非常感谢!

Views.py - ToolList

class ToolList(SearchableListMixin, SortableListMixin, ListView):
    model = ToolCalibration
    template_name = 'tool_cal/list.html'
    paginate_by = 10
    context_object_name = 'tools'
    search_fields = ['description', 'notes', 'tolerance_notes']
    sort_fields_aliases = [
        ('description', 'by_description'), 
        ('last_certified', 'by_last_certified'), 
        ('due_date', 'by_due_date'), 
        ('tool_status', 'by_tool_status'), 
        ]

    def get_queryset(self, **kwargs):
        qs = super(ToolList, self).get_queryset()
        print(self.kwargs)
        sort = 'due_date'
        ### PROBLEM IS HERE. FOR SOME REASON KWARGS ISN'T GRABBING GET VARIABLES ###
        if 'sort' in self.kwargs:
            sort = self.kwargs['sort']
        try:
            schema = self.kwargs['sch']
            print 'Context using %s' % schema
            return qs.filter(schema__abbrev=schema).order_by(sort)
        except:
            print 'Context contains no schema'
            return qs.order_by(sort)

    def get_context_data(self, **kwargs):
        context = super(ToolList, self).get_context_data(**kwargs)
        context['enterprise'] = EnterpriseSchema.objects.all()
        return context
Run Code Online (Sandbox Code Playgroud)

Rod*_*ier 6

GET可以使用request对象访问参数.该request对象具有一个属性GET,一个QueryDict实例,它是一个类似字典的对象.同样,POST也可以使用request对象访问参数.在Django 1.7之前,您可以使用该属性访问GETPOST参数REQUEST.这已被弃用,因为最好使用更明确的GETPOST属性.

# GET parameters
self.request.GET.get('sort')
self.request.GET.get('sch')

# POST parameters
self.request.POST.get('key')

# Prior to Django 1.7, you can do
self.request.REQUEST.get('key') # this is either a GET or POST parameter
Run Code Online (Sandbox Code Playgroud)