我想将两个参数传递给Django API的端点。这是我正在做的第一个Django API。目前,我在data = {'param1':[0.4],'param2':[0.9]}
中对输入参数进行了硬编码。
然后,我希望能够如下http://localhost:8000&lat=50¶m2=30
我应该如何更新view.py
的代码以获得所需的功能?
from django.http import HttpResponse
import pandas as pd
import json
# used to export a trained model
from sklearn.externals import joblib
def index(request):
decision_tree = joblib.load('proj/model/decision_tree.pkl')
# now I manually pass data, but I want to get it from request
data = {'param1':[0.4],'param2':[0.9]}
test_X = pd.DataFrame(data)
y_pred = decision_tree.predict(test_X)
response_data = {}
response_data['prediction'] = y_pred
response_json = json.dumps(response_data)
return HttpResponse(response_json)
答案 0 :(得分:1)
您可以使用url query string。如果您使用http://localhost:8000?param1=50¶m2=30
,则可以这样访问它们:
def index(request):
param1 = request.GET.get('param1')
param2 = request.GET.get('param2')
# rest of the code