2017-04-10 9 views
1

私はdjangoとpythonについて完全に新しくなっています。今、私は簡単なサービスを開発しています。これはアイデアです:私はPOSTからDjangoのDjangoに3つのパラメータを送信します(別のドメイン、CORSから)Djangoで、Pythonのデータを処理してJSONを返します。DjangoサービスからJSONデータを取得する

どうすればいいですか?なぜなら私はpyhon上で利用できる特別な機能が必要だからです。

これは私が始まるコードです:これは、CodeIgniterのかlaravelに非常に単純である

urls.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)] 

controlador.py

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

。私はdjangoでこれをどのように始めるのか分かりません。

どうすればいいですか?

ありがとうございます! ロジー

答えて

0

renderは、辞書の変数をテンプレートに「コンテキスト」として送信することを前提としています。 get_data_weibullビューでは、通常はrequestと呼ばれる1つの変数を受け入れる必要があります(selfは、オブジェクトの関数の最初の引数の名前としてのみ使用されます)。次に、あなたのラインデータで辞書を構築し、それを'JsonResponse`として返すことができます。

0

ています。この

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

またはあなたのジャンゴの古いバージョンを使用している場合、あなたはこの代わりに

HttpResponse(json.dumps(line.get_xydata()), content_type="application/json") 
を返すことができます。
関連する問題