我想通过onclick执行我的python代码。运行服务器后我得到了结果。我的按钮不工作。这是我的代码。如何通过django html按钮执行python代码?
网址 -
url(r'^index/$', index),
index.html-
<html>
<body>
<form action="/index/" method="GET">
<input type="submit" value="Click">
</form>
{{output}}
</body>
</html>
views.py -
from django.shortcuts import render, render_to_response
from pythoncode import mycode
def index(request):
if request.method=="GET":
py_obj=mycode.test_code(10)
py_obj.code()
return render(request, 'index.html', {"output":py_obj.a})
我创建了一个更多的应用到单独的Python代码 - 应用程序名称是蟒蛇代码和文件名mycode
class test_code:
def __init__(self, a):
self.a=a
self.b=4
def code(self):
return self.a, self.b
请帮帮我。我是Django的新手。在此先感谢
请格式化您的代码 – Brandon 2014-10-10 12:32:09
究竟是什么错误? – Brandon 2014-10-10 12:57:26
我想在点击“Click”按钮后执行mycode.py文件,并在模板上显示输出10。在上面的示例中,当我重新加载页面时,我获得的输出是10。 – user3891137 2014-10-10 13:04:53