Django 2 -403禁止错误:无法从views.py发出获取请求

时间:2019-09-05 16:57:07

标签: python django post get django-views

我想从views.py中的功能启用GET请求并读取数据。我的功能在views.py中,它是在POST请求后触发的。

Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/lib/python3.4/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/lib/python3.4/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/rhtry/redirect/redirect/views.py", line 174, in paysuccess
    response = urllib.request.urlopen(request)
  File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 470, in open
    response = meth(req, response)
  File "/usr/lib/python3.4/urllib/request.py", line 580, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.4/urllib/request.py", line 508, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 442, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 588, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

跟踪显示csrf.py文件中的问题。

ubuntu@ip-172-31-19-128:~$ aws s3api list-objects --bucket s3-knowledge-base --query 'Contents[?LastModified>=`2019-05-21T14:38:07.000Z`].{Key : Key, LastModified: LastModified }'

[
    {
        "Key": "images/tnats/connectivity.JPG",
        "LastModified": "2019-05-21T14:38:07.000Z"
    },
    {
        "Key": "images/tnats/gsk-logo.png",
        "LastModified": "2019-05-29T16:58:46.000Z"
    }
]

1 个答案:

答案 0 :(得分:2)

您可能需要设置用户代理标头,以使网站认为您正在使用浏览器。代码如下:

@csrf_exempt
def paysuccess(request):

    URL ="http://example/apiusername=111390&pwd=123&circlecode=2&format=json"

    request=urllib.request.Request(URL, headers={'User-Agent': 'Mozilla/5.0'}) 
    response = urllib.request.urlopen(request)