BAE
BAE

Reputation: 8936

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',
]

Upvotes: 7

Views: 13397

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

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.

Upvotes: 8

Related Questions