Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Django服务获取JSON数据_Python_Json_Ajax_Django_Request - Fatal编程技术网

Python 从Django服务获取JSON数据

Python 从Django服务获取JSON数据,python,json,ajax,django,request,Python,Json,Ajax,Django,Request,我对django和python一无所知。现在我正在尝试开发一个简单的服务。这就是我的想法:我通过POST将3个参数从JS发送到Django(来自另一个域CORS),在Django中,python处理数据并返回JSON,所有这些 我为什么要这么做,因为我需要pyhon:Statistics上的特殊功能 这是我开始的代码: url.py from django.conf.urls import url from django.contrib import admin from

我对django和python一无所知。现在我正在尝试开发一个简单的服务。这就是我的想法:我通过POST将3个参数从JS发送到Django(来自另一个域CORS),在Django中,python处理数据并返回JSON,所有这些

我为什么要这么做,因为我需要pyhon:Statistics上的特殊功能

这是我开始的代码:

url.py

   from django.conf.urls import url
    from django.contrib import admin
    from . import controlador #este sisi

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^get_weibull/', controlador.get_data_weibull)]
  from django.shortcuts import render
    from django.http import HttpResponseRedirect
    from django.shortcuts import render_to_response
    import numpy as np
    import matplotlib.pyplot as plt

    def weib(x,n,a):
        return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)

    def get_data_weibull(self):
        a = 5. # shape
        s= np.random.weibull(a, 1000)
        x = np.arange(1,100.)/50.
        count, bins, ignored = plt.hist(np.random.weibull(5.,1000))
        x = np.arange(1,100.)/50.
        scale = count.max()/weib(x, 1., 5.).max()
        plt.plot(x, weib(x, 1., 5.)*scale)

        ax = plt.gca() 
        line = ax.lines[0]
        return render_to_response(line.get_xydata())
controlador.py

   from django.conf.urls import url
    from django.contrib import admin
    from . import controlador #este sisi

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^get_weibull/', controlador.get_data_weibull)]
  from django.shortcuts import render
    from django.http import HttpResponseRedirect
    from django.shortcuts import render_to_response
    import numpy as np
    import matplotlib.pyplot as plt

    def weib(x,n,a):
        return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)

    def get_data_weibull(self):
        a = 5. # shape
        s= np.random.weibull(a, 1000)
        x = np.arange(1,100.)/50.
        count, bins, ignored = plt.hist(np.random.weibull(5.,1000))
        x = np.arange(1,100.)/50.
        scale = count.max()/weib(x, 1., 5.).max()
        plt.plot(x, weib(x, 1., 5.)*scale)

        ax = plt.gca() 
        line = ax.lines[0]
        return render_to_response(line.get_xydata())
这在codeigniter或laravel上非常简单。但我不知道如何在django上开始

我该怎么做

谢谢!
Rosie

render_to_response
,它已被替换/替换为希望将字典中的变量作为“上下文”发送到模板。您的
get\u data\u weibull
视图应接受一个变量,通常称为
request
self
仅用作对象函数的第一个参数的名称)。然后,您可以使用行数据构建一个字典,并将其作为。

render\u to\u响应返回,该响应已被替换/替换为Expected,以将字典中的变量作为“上下文”发送到模板。您的
get\u data\u weibull
视图应接受一个变量,通常称为
request
self
仅用作对象函数的第一个参数的名称)。然后,您可以使用行数据构建一个字典,并将其作为一个字符串返回。

执行此操作

from django.http import JsonResponse


def get_data_weibull(self):
    a = 5. # shape
    s= np.random.weibull(a, 1000)
    x = np.arange(1,100.)/50.
    count, bins, ignored = plt.hist(np.random.weibull(5.,1000))
    x = np.arange(1,100.)/50.
    scale = count.max()/weib(x, 1., 5.).max()
    plt.plot(x, weib(x, 1., 5.)*scale)

    ax = plt.gca() 
    line = ax.lines[0]
    return JsonResponse(line.get_xydata())
或者,如果您使用的是Django的旧版本,则可以返回此文件

HttpResponse(json.dumps(line.get_xydata()), content_type="application/json")
这样做

from django.http import JsonResponse


def get_data_weibull(self):
    a = 5. # shape
    s= np.random.weibull(a, 1000)
    x = np.arange(1,100.)/50.
    count, bins, ignored = plt.hist(np.random.weibull(5.,1000))
    x = np.arange(1,100.)/50.
    scale = count.max()/weib(x, 1., 5.).max()
    plt.plot(x, weib(x, 1., 5.)*scale)

    ax = plt.gca() 
    line = ax.lines[0]
    return JsonResponse(line.get_xydata())
或者,如果您使用的是Django的旧版本,则可以返回此文件

HttpResponse(json.dumps(line.get_xydata()), content_type="application/json")