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)
1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.