简体   繁体   中英

Django REST framework: 'WSGIRequest' object has no attribute 'query_params'

I am learning DRF , and trying to print print request.query_params . But got error:

    print request.query_params
AttributeError: 'WSGIRequest' object has no attribute 'query_params'

Codes:

class CourseDetailView(generics.RetrieveAPIView):
    queryset = Course.objects.all()
    serializer_class = CourseSerializer

    def dispatch(self, request, *args, **kwargs):
        print request.user
        print 'CourseDetailView dispatch:', request.META
        #print request.data
        """
        print 'parsers', request.parsers
        print request.accepted_renderer
        print 'authenticators', request.authenticators
        """
        #print 'accepted_media_type', request.accepted_media_type
        print request.META['HTTP_ACCEPT']
        print 'method', request.method
        print 'content_type', request.content_type
        print 'query_params'
        print request.query_params # Here

        return super(CourseDetailView, self).dispatch(request, *args, **kwargs)

Part of my settings:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'api',
]

# DJANGO REST FRAMEWORK
REST_FRAMEWORK = {
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

dispatch is the first method called in a class based view, and it's where all the setup happens - including, in the case of an API view, the wrapping of the Django request object with the DRF extensions. By overriding it you've prevented any of that code from running, so you just have the basic Django request.

Generally you should avoid overriding dispatch . Use a more appropriate method, eg get .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM