0

I am using a JQuery chart library where you pass values a format like [0,2,5,9].

I have an array in my views where I currently access each index to return the value rest 1 = arr[0], rest 2 = arr[1] ... and then passing these values from the view into my HTML page and inserting the values for the chart like [res1,res2]. This is not feasible because I never know the size of the array so it'd be a constant manual approach of accessing the array. Is there a way I easily loop through each one?

Slight problem. Currently after accessing each index I convert value to an int. So I don't think I'd be able to do it via looping through the html page - it'd have to be done via views. Unless I can somehow call conversion function defined in the view in the html page?

--- views --- 
array

 -> returns multiple string values i.e. name = paul, name = john
-> paul = arr[0] 
-> function(paul['']) converts it to a string.

--- html page ---
{{paul}}

ideally i'd like to do:

[rather than refer to each index here just loop through all array values.. but somehow calling my conversion function too on each value else what I want to do won't work. ]

 {% for values in array %}
[insert each one here and call the convert int function from views] 

1 Answer 1

2

I might misunderstood your question, but sounds like you just want to pass a list of string to the template, loop on each item in the list and do some conversion. I'm not really sure which step did you get stuck but the simplest way is to do everything in the views.py:

views.py

def view_func(request):
    array =  [{'Speed': 2, 'Height': 1, 'PersonID': 1}, {'Speed': 2, 'Height': 1, 'PersonID': 1}]
    # do the conversion on each value in the list
    converted_array = [int(i['speed']]) for i in array]
    context = {'array': array, 'converted_array': converted_array}

template:

<!-- to loop on original array -->
{% for value in array %}
    {{ value }}
{% endfor %}

<!-- to loop on the converted array -->
{% for value in converted_array %}
    {{ value }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

8 Comments

hey, yeah you have the right idea. Problem is though I don't think my function for converting to int is working with an array, but only when I do it on an index of the array; as I am getting ; 'QuerySet' object does not support item assignment. My function is simply def name(value): return (int(value))
I edited my answer. You don't need your function at all, just do int(i).
sorry it's a dict not a String so ; int() argument must be a string, a bytes-like object or a number, not 'dict'
OK but the fix is easy enough, you just convert whatever it's in your dict, right? Do you have an example of what each dict looks like?
yeah so it's like [{'Speed': 2, 'Height': 1, 'PersonID': 1}, {'Speed': 2, 'Height': 1, 'PersonID': 1}] and I only need one of those values say for example Speed - > I just need the int value
|

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.