mardi 4 août 2015

Sending Python array to django template by AJAX

Im trying to send a random Python array to a template in Django by using AJAX:

JS:

    $("button").click(function() {
        $.ajax({
            url: "/hello",
            type: "get",
            success: function(data) { 
                printData(data);                    
            },
            error: function(data) { 
                alert("Error!");
            }
        });
    })

Django view:

from django.http import HttpResponse
from django.views.generic import View
from django.shortcuts import render, render_to_response

import numpy as np

def hello(request):
    rand_arr = np.random.randint(100, size=10)
    return HttpResponse(rand_arr)

I receive the array as a string like: 3133352430290167691. Is it possible to receive it as an array, and access the individual values?

Aucun commentaire:

Enregistrer un commentaire