2015-01-17 57 views
-1

我正在嘗試創建一個使用3個數字輸入計算正常密度的代碼。我試着運行下面的代碼,但網站沒有收到輸入信息,跳到最後一條評論'謝謝你'。我怎麼修復它?提前致謝!獲取數字輸入並計算

from django.shortcuts import render 
from django import forms 
from django.http import * 
from scipy.stats import norm 

class NF(forms.Form): 

    n1 = forms.FloatField(label = 'Input a Number') 
    n2 = forms.FloatField(label = 'Mean') 
    n3 = forms.FloatField(label = 'SD') 


def cal(request): 
    if request.method == 'POST': 
     form = NF(request.POST) 
     if form.is_valid(): 
      number = form.clean_data['n1'] 
      mean = form.clean_data['n2'] 
      sd = form.clean_data['n3'] 
      density = norm.pdf(number, mean, sd) 
      html= '<html><body>The density is %s.</body></html>' % density 

    return HttpResponse('Thanks!') 
+0

「的網站不接受輸入」 - 你什麼意思?你有沒有發現表單數據是'None'?你確定你發送的數據正確嗎? – hichris123

+0

我是一個真正的新手,所以我不知道。 '表單數據是無'是什麼意思? –

+0

在Python中沒有任何意義。您還需要檢查數據是否等於空字符串,即''「'。通過表單數據,我的意思是'號碼','平均值'和'sd'。 – hichris123

回答

0

我想這可能是因爲你沒有返回你得到的東西,而只是一個謝謝。

更改最後一行是:

... 
      html= '<html><body>The density is %s.</body></html>' % density 

    return HttpResponse(html) 
...