Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django 2-403禁止的错误:无法从views.py发出get请求_Python_Django_Post_Get_Django Views - Fatal编程技术网

Python Django 2-403禁止的错误:无法从views.py发出get请求

Python Django 2-403禁止的错误:无法从views.py发出get请求,python,django,post,get,django-views,Python,Django,Post,Get,Django Views,我想在views.py中启用从函数获取请求并读取数据。我的函数位于views.py中,该函数在POST请求时触发 @csrf_exempt def paysuccess(request): #process requests URL ="http://example/apiusername=111390&pwd=123&circlecode=2&format=json" #make get request request=urllib.re

我想在views.py中启用从函数获取请求并读取数据。我的函数位于views.py中,该函数在POST请求时触发

@csrf_exempt
def paysuccess(request):
    #process requests
    URL ="http://example/apiusername=111390&pwd=123&circlecode=2&format=json"
    #make get request
    request=urllib.request.Request(URL) 
    response = urllib.request.urlopen(request)
    #read response 
回溯显示csrf.py文件中的问题

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_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)

您可能需要设置用户代理标题,使站点认为您正在使用浏览器。代码将是:

@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)

您正在调用外部API(此
URL
是第三方)?“禁止”来自您正在获取的URL,因此这与csrf无关。您将视图设置为csrf_豁免的事实与您调用的API返回HTTP 403的事实无关。您正在调用外部API(此
URL
是第三方)?“禁止”来自您正在获取的URL,因此这与csrf无关。您将视图设置为csrf_豁免的事实与您调用的API返回HTTP 403的事实没有任何关系。仍然会收到相同的错误。urllib.error.HTTPError:HTTP错误403:Forbidden@MohitKumar你想连接什么网站?@MohitKumar nice!仍然得到相同的错误。urllib.error.HTTPError:HTTP错误403:Forbidden@MohitKumar你想连接什么网站?@MohitKumar nice!