0

I want to pass two parameters to the end-point of my Django API. This is the first Django API that I am doing. Currently I hardcoded the input parameters in data = {'param1':[0.4],'param2':[0.9]}.

Then I want to be able to call this end-point as follows http://localhost:8000&lat=50&param2=30

How should I update this code of view.py in order to obtained the desired functionality?

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)
ScalaBoy
  • 3,254
  • 13
  • 46
  • 84

1 Answers1

1

You can use url query string for this. If you use http://localhost:8000?param1=50&param2=30, then you can access them like this:

def index(request):
    param1 = request.GET.get('param1')
    param2 = request.GET.get('param2')
    # rest of the code
ruddra
  • 50,746
  • 7
  • 78
  • 101