不知道如何讀取方程式並顯示出來...

0 views
Skip to first unread message

柯俊宏

unread,
Apr 17, 2008, 9:13:22 AM4/17/08
to AskHoamon
搞不懂方程式應該寫在哪,它才會正常顯示
目前讀取樣板正常ok.....但按下送出,卻毫無反應


from django.template import Context, loader
from django.template.loader import get_template
from django.http import HttpResponse
from numpy import *
import os

#def my_sin_form ( request) :
# #a=sin(x)
# ans=sin(int(request['x']))+cos(int(request['y']))
# return HttpResponse(str(ans))
#def get(R,x,y):

def get_value(R,x,y):
fun=sin(x)+cos(y)
sum=eval(fun.lower())
return HttpResponse(sum)


def my_sin_form (R):
t = get_template(os.path.join('my_sin', 'index.html'))
if not R.POST.has_key('submit'):
html = t.render({})
else:
html = t.render({
'x': int(R.POST['x']),
'y': int(R.POST['y']),
})

return HttpResponse(html)

阿蒙

unread,
Apr 17, 2008, 9:26:58 AM4/17/08
to AskHoamon
On 4月17日, 下午9時13分, 柯俊宏 <coericsh...@gmail.com> wrote:
> 搞不懂方程式應該寫在哪,它才會正常顯示
> 目前讀取樣板正常ok.....但按下送出,卻毫無反應
其實我不太懂你要問的問題。是顯示方程式的圖嗎?你下面的程式沒有用 PIL 所以不可能有方程式的圖出現。
你說毫無反應,那請問你的預期反應是什麼?

你用的是 http://127.0.0.1:8000/ 來作測試嗎??

>
> from django.template import Context, loader
> from django.template.loader import get_template
> from django.http import HttpResponse
> from numpy import *
> import os
>
> #def my_sin_form ( request) :
> # #a=sin(x)
> # ans=sin(int(request['x']))+cos(int(request['y']))
> # return HttpResponse(str(ans))
> #def get(R,x,y):
>
> def get_value(R,x,y):
> fun=sin(x)+cos(y)
假定你的 x = 0, y = 0 則 fun = 1
eval(1)的結果還是 1 。

所以你的想法應該是 fun = 'sun(%s)+cos(%s)' % (x, y) 這樣才對,eval 所代入的值是字串才對。

> sum=eval(fun.lower())
> return HttpResponse(sum)
>
> def my_sin_form (R):
> t = get_template(os.path.join('my_sin', 'index.html'))
> if not R.POST.has_key('submit'):
> html = t.render({})
> else:
> html = t.render({
> 'x': int(R.POST['x']),
> 'y': int(R.POST['y']),
> })
>
> return HttpResponse(html)
這個函式,只是把你所輸入的值秀出來而已。但你在樣版中有設定 {{ x }}, {{ y }} 嗎

柯俊宏

unread,
Apr 17, 2008, 3:52:19 PM4/17/08
to AskHoamon
我沒有要用圖表示,只是單純想將fin=sin(x)+cos(y) 的結果顯示出來即可
我是用http://127.0.0.1:8000/ 來測試沒錯
樣板有設定{{x}}、{{y}}
Reply all
Reply to author
Forward
0 new messages