【发布时间】:2014-12-05 15:03:37
【问题描述】:
我想通过 onclick 执行我的 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 代码 - 应用程序名是python代码,文件名是mycode
class test_code:
def __init__(self, a):
self.a=a
self.b=4
def code(self):
return self.a, self.b
请帮助我。我是 Django 的新手。提前致谢
【问题讨论】:
-
请格式化您的代码
-
究竟是什么错误?
-
我想在单击“单击”按钮后执行 mycode.py 文件并在模板上显示输出 10。在上面的示例中,当我重新加载页面时,我得到了输出 10。
-
根据您的看法,
{{ output }}的值将存在于每个 GET 请求中,而不仅仅是在您提交表单之后。如果您的 HTML 是准确的,那么您在 标记上缺少右尖括号。
标签: html django django-views