Django REST框架:“ WSGIRequest”对象没有属性“ query_params” [英] Django REST framework: 'WSGIRequest' object has no attribute 'query_params'
问题描述
我正在学习 DRF ,并尝试打印打印request.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'
代码:
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)
我的部分设置:
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
是在基于类的视图中调用的第一个方法,它是所有设置发生的地方-包括(对于API视图而言)使用DRF包装Django请求对象扩展名。通过覆盖它,您已经阻止了任何代码的运行,因此您只有基本的Django请求。
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.
通常,您应该避免覆盖 dispatch
。使用更合适的方法,例如 get
。
Generally you should avoid overriding dispatch
. Use a more appropriate method, eg get
.
这篇关于Django REST框架:“ WSGIRequest”对象没有属性“ query_params”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!