我已经开发了带有参数的python脚本。 它具有根据收到的参数执行不同操作的功能。 现在,我试图通过API传递这些参数。 我正在使用Django 2.0的path()新功能(具体来说,这是我正在使用的版本:2.2.9) 在Django中,传递传递到url中的方式是什么? 正在使用整数,但不能使用浮点数。
通过浮动的最佳方法是什么?您会转换为十六进制吗?
使用GET指向API并执行任务的示例:
Not wrking: http://x.x.x.x:8000/proveedores/api/pruebascelery/buy/0/1.1287 (last parameters is a float)
works with interger: http://x.x.x.x:8000/proveedores/api/pruebascelery/buy/0/2
Django:(urls.py) 路径('pruebascelery ///',lanzador_task_start),
Django:(views.py)
def lanzador_task_start(request, post_id, post_id2, post_id3):
comando = "python /home/developer/venvpruebas/recibirparametros.py " + str(post_id) +str(" ")+
str(post_id2)+str(" ")+ str(post_id3)
lanzar.delay(comando)
return HttpResponse('<h1>Done</h1')
python端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import struct
def RecibirParametros(recibir_todo):
print("Received: ", recibir_todo)
if __name__ == "__main__":
recibir_todo = sys.argv[1], sys.argv[2], sys.argv[3]
#--------------------------------------------------------------
#[1] test1
#[2] test2
#[3] test3
#--------------------------------------------------------------
RecibirParametros(recibir_todo)
You can use Path converters for customize url parameters.
在converts.py中
在urls.py中
对于替代方法:
You can use regular expressions inside urls.py directly with
re_path
function like that:另外,您可以从url检索参数作为查询参数。 网址示例: