我希望在任何用戶發出諸如創建匯出洗掉或更新之類的任何請求時創建新資料,但現在我正在處理匯出。 audit_logs url) 用戶的資料,當任何用戶下載資料的 pdf 格式后將創建該資料..所以基本上我希望顯示哪個用戶在什么時間以及他執行的哪個操作將在 audit_logs url 上獲取它
我正在資料/視圖中發出請求,但在 /audit_logs/create/ 處收到錯誤錯誤請求
視圖.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())**
當我發布回復時,它會給我以下回復
{'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 <Response [400] > {'username': ['這個欄位是必需的。'],'action_type':['這個欄位是必需的。'],'action':['這個欄位是必需的。'],'ip_address':['此欄位是必需的。']}
uj5u.com熱心網友回復:
當你requests在你的情況下使用post 時,你需要 passdata而不是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與關鍵 值的params字典。
uj5u.com熱心網友回復:
你應該看看 djangorestframework https://www.django-rest-framework.org/tutorial/2-requests-and-responses/
這是使用 django 撰寫 API 的推薦方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357634.html
