如何在 Django 中使用 api 创建 (POST) 请求
How to make create (POST) request using api in django
我希望在任何用户发出诸如创建导出删除或更新之类的任何请求时创建新数据,但现在我正在处理导出..我有两个 urls 一个有表格数据( average_data urls) 和一个 url 具有 (audit_logs url) 用户数据,当任何用户下载 pdf 格式后将创建该数据的数据.. 所以基本上我希望显示哪个用户在什么时间和他做了什么动作会在 audit_logs url
上得到它
我正在 data/views 中提出 post 的请求,但在 /audit_logs/create/
收到错误的错误请求
views.py
def averagePDF(request):
global fromDate
global toDate
global site
global device
global parameters
global interval
global data
headers = {
'authorization': "Bearer X...............",
}
devices_url = "http://127.0.0.1:8000/stations/list"
devices_params = {
'id': device
}
devices = requests.request("GET", devices_url, headers=headers, params=devices_params)
response = HttpResponse()
response['Content-Disposition'] = 'attachment; filename=Average Data.pdf'
elements = []
company_name = Paragraph(devices.json()[0]['site'], header_style)
elements.append(company_name)
report_data = Paragraph("Date: " + fromDate + " to " + toDate + " Station: " + devices.json()[0]['station'], title_style)
elements.append(report_data)
styles = getSampleStyleSheet()
styleN = styles['Normal']
styleN.wordWrap = 'CJK'
file_data = []
header = []
header.append("Timestamp")
header.append("Station")
for parameter in parameters:
header.append(parameter)
file_data.append(header)
data2 = [[Paragraph(cell, styleN) for cell in row] for row in file_data]
width = (PAGE_WIDTH-50) / len(header)
table_header = Table(data2, colWidths=width, style=table_header_style)
elements.append(table_header)
table_data = []
for key, values in data.items():
raw_data = []
raw_data.append(str(key))
raw_data.append(devices.json()[0]['station'])
for value in values:
raw_data.append(str(value))
table_data.append(raw_data)
table_data2 = [[Paragraph(cell, styleN)for cell in row] for row in table_data]
tableRaw = LongTable(table_data2, colWidths=width, style=table_style)
elements.append(tableRaw)
doc.title = "Average Data"
meta_data = request.META.get('HTTP_X_FORWARDED_FOR')
if meta_data:
ip = meta_data.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR')
**now=datetime.datetime.now()
# date_time = now.strftime('%Y-%m-%dT%H:%M:%S.%f')
username=str(request.user)
action_type="Export"
action="Export"
ip_address=ip
audit_url="http://127.0.0.1:8000/audit_logs/create/"
audit_parms={
"username":username,
"action_type":action_type,
"action":action,
"ip_address":ip_address
}
audit_obj=requests.post(audit_url, headers=headers, params=audit_parms)
print(audit_obj.json())**
当我post回复时,它会给我以下回复
{'username': 'abcpadmin', 'action_type': 'Export', 'action': 'Export', 'ip_address': '127.0.0.1'}
错误请求:/audit_logs/create/
[15/Nov/2021 16:08:24] "POST /audit_logs/create/?username=abcpadmin&action_type=Export&action=Export&ip_address=127.0.0.1 HTTP/1.1" 400 160
<响应[400]>
{'username': ['This field is required.'], 'action_type': ['This field is required.'], 'action': ['This field is required.'], 'ip_address': [ 'This field is required.']}
你应该看看 djangorestframework
https://www.django-rest-framework.org/tutorial/2-requests-and-responses/
这是使用 django 编写 API 的推荐方式。
当你使用 requests
作为 post 在你的情况下,你需要传递 data
而不是 params
应该是:
audit_obj=requests.post(audit_url, headers=headers, data=audit_parms)
技术上,当您执行 requests.post(url=your_url, params=your_params)
时,url 会像 https://localhost?key=value
和 key params
字典中的值。
我希望在任何用户发出诸如创建导出删除或更新之类的任何请求时创建新数据,但现在我正在处理导出..我有两个 urls 一个有表格数据( average_data urls) 和一个 url 具有 (audit_logs url) 用户数据,当任何用户下载 pdf 格式后将创建该数据的数据.. 所以基本上我希望显示哪个用户在什么时间和他做了什么动作会在 audit_logs url
上得到它我正在 data/views 中提出 post 的请求,但在 /audit_logs/create/
收到错误的错误请求views.py
def averagePDF(request):
global fromDate
global toDate
global site
global device
global parameters
global interval
global data
headers = {
'authorization': "Bearer X...............",
}
devices_url = "http://127.0.0.1:8000/stations/list"
devices_params = {
'id': device
}
devices = requests.request("GET", devices_url, headers=headers, params=devices_params)
response = HttpResponse()
response['Content-Disposition'] = 'attachment; filename=Average Data.pdf'
elements = []
company_name = Paragraph(devices.json()[0]['site'], header_style)
elements.append(company_name)
report_data = Paragraph("Date: " + fromDate + " to " + toDate + " Station: " + devices.json()[0]['station'], title_style)
elements.append(report_data)
styles = getSampleStyleSheet()
styleN = styles['Normal']
styleN.wordWrap = 'CJK'
file_data = []
header = []
header.append("Timestamp")
header.append("Station")
for parameter in parameters:
header.append(parameter)
file_data.append(header)
data2 = [[Paragraph(cell, styleN) for cell in row] for row in file_data]
width = (PAGE_WIDTH-50) / len(header)
table_header = Table(data2, colWidths=width, style=table_header_style)
elements.append(table_header)
table_data = []
for key, values in data.items():
raw_data = []
raw_data.append(str(key))
raw_data.append(devices.json()[0]['station'])
for value in values:
raw_data.append(str(value))
table_data.append(raw_data)
table_data2 = [[Paragraph(cell, styleN)for cell in row] for row in table_data]
tableRaw = LongTable(table_data2, colWidths=width, style=table_style)
elements.append(tableRaw)
doc.title = "Average Data"
meta_data = request.META.get('HTTP_X_FORWARDED_FOR')
if meta_data:
ip = meta_data.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR')
**now=datetime.datetime.now()
# date_time = now.strftime('%Y-%m-%dT%H:%M:%S.%f')
username=str(request.user)
action_type="Export"
action="Export"
ip_address=ip
audit_url="http://127.0.0.1:8000/audit_logs/create/"
audit_parms={
"username":username,
"action_type":action_type,
"action":action,
"ip_address":ip_address
}
audit_obj=requests.post(audit_url, headers=headers, params=audit_parms)
print(audit_obj.json())**
当我post回复时,它会给我以下回复
{'username': 'abcpadmin', 'action_type': 'Export', 'action': 'Export', 'ip_address': '127.0.0.1'}
错误请求:/audit_logs/create/ [15/Nov/2021 16:08:24] "POST /audit_logs/create/?username=abcpadmin&action_type=Export&action=Export&ip_address=127.0.0.1 HTTP/1.1" 400 160 <响应[400]> {'username': ['This field is required.'], 'action_type': ['This field is required.'], 'action': ['This field is required.'], 'ip_address': [ 'This field is required.']}
你应该看看 djangorestframework https://www.django-rest-framework.org/tutorial/2-requests-and-responses/
这是使用 django 编写 API 的推荐方式。
当你使用 requests
作为 post 在你的情况下,你需要传递 data
而不是 params
应该是:
audit_obj=requests.post(audit_url, headers=headers, data=audit_parms)
技术上,当您执行 requests.post(url=your_url, params=your_params)
时,url 会像 https://localhost?key=value
和 key params
字典中的值。