Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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 如何在需要登录另一个url后将文件发送到网站?_Python_Django_Python 3.x_Python Requests - Fatal编程技术网

Python 如何在需要登录另一个url后将文件发送到网站?

Python 如何在需要登录另一个url后将文件发送到网站?,python,django,python-3.x,python-requests,Python,Django,Python 3.x,Python Requests,我正在制作一个Pyqt5应用程序,我现在正试图让它通过点击按钮在我的django网站上发布一个文件。我可以使用请求让脚本登录到我的站点,没有问题(使用csrftoken),但是当上载文件(到“”)时,它会引发403禁止错误(“未设置CSRF cookie”),即使我将csrftoken与文件一起发送 代码如下: 发送_file.py 如果您需要查看django模型、模板或与网站相关的任何内容:发送文件的post请求正在使用与其他请求不同的会话,requests.post,因此不会设置cookie

我正在制作一个
Pyqt5
应用程序,我现在正试图让它通过点击按钮在我的django网站上发布一个文件。我可以使用
请求
让脚本登录到我的站点,没有问题(使用csrftoken),但是当上载文件(到“”)时,它会引发403禁止错误(“未设置CSRF cookie”),即使我将csrftoken与文件一起发送

代码如下:

发送_file.py


如果您需要查看django模型、模板或与网站相关的任何内容:

发送文件的post请求正在使用与其他请求不同的会话,
requests.post
,因此不会设置cookie

改为尝试
client.post

r = client.post('http://127.0.0.1:8000/ranking/', files={'training_data.npy': f}, data=file_data, headers=headers)

您是否尝试在标头上发送(第二个)令牌
X-CSRFToken
@fixmycode像这样<代码>标题={“Referer”:http://127.0.0.1:8000/ranking/“,”X-CSRFToken“:csrftoken1}谢谢!还需要将“training data.npy”更改为“file”(我认为这是html默认名称)
r = client.post('http://127.0.0.1:8000/ranking/', files={'training_data.npy': f}, data=file_data, headers=headers)