[英]AttributeError: 'WSGIRequest' object has no attribute 'get' while making a get request
I am trying to make a get request to stackoverflow API but I am facing a WSGI error while making the requests through POSTMAN.我正在尝试向 stackoverflow API 发出获取请求,但在通过 POSTMAN 发出请求时遇到 WSGI 错误。 Here is the code snippet:
这是代码片段:
Views:意见:
def search(requests):
query = requests.GET["query"]
response = requests.get('https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + query + '&site=stackoverflow')
api_Data = response.json()
return JsonResponse({"message": api_Data})
URLs:网址:
urlpatterns = [
path('search/', views.search, name='search'),
]
I have tried I keep getting 'WSGIRequest' object has no attribute 'Get' on django but it did not work.我试过我不断收到'WSGIRequest' object 在 django 上没有属性'Get',但它没有用。
You're running into trouble here because you (I am assuming) have imported python-requests
as requests
earlier in your file, but you are overriding that by naming the HTTPRequest
object that your view get as requests
too.您在这里遇到了麻烦,因为您(我假设)已将
python-requests
作为requests
导入到文件的前面,但是您通过将您的视图也作为requests
获得的HTTPRequest
object 命名来覆盖它。
Here's a fixed version, by naming the argument to the view function simply as request
(which is more common in Django, too)这是一个固定版本,通过将视图 function 的参数命名为
request
(这在 Django 中也更常见)
import requests # note the name
...
# variable name changed
def search(request):
# and changed here too, as well as allowing for "query" not to be present in `request.GET`
query = request.GET.get("query")
response = requests.get('https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + query + '&site=stackoverflow')
api_Data = response.json()
return JsonResponse({"message": api_Data})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.