0

I have a problem calling a function with string parameters obtained from python.

python_var -> html calls the function of js with parameters obtained from python context -> js file

Browser console says: Uncaught ReferenceError
I am using Django template language to get the variable which is defined as a string in python.

foo.html

<script>
 libcharts_predict_k( {{title|safe}}, {{data|safe}})
</script>

I am sure the problem is only with Strings in JS function parameters because with arrays and numbers I didn't have any problem.
Browser tries to reference something but I am giving as a value

view.py

def second_view(request):
    title = "A long title"
    data= df['value'].values.tolist()
    context = {
               'data': data,
               'title': title
               }
return (request, 'app/foo.html', context=context)

*(Imports are fine)

1 Answer 1

1

Short answer: put quotes around your template vars:

libcharts_predict_k("{{title|safe}}", "{{data|safe}}")

And since data is a list you want to jsonify it before (in your view):

import json

# ....

data = json.dumps(df['value'].values.tolist())
Sign up to request clarification or add additional context in comments.

2 Comments

Ok I tried python libcharts_predict_kilos_ventas_familia( '{{titulo|safe}}',{{data|safe}}) and it works, thanks for the idea!
cf the edit (added after you posted your view code)

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.