我希望通过点击按钮来执行我的Python代码。在运行服务器后,我可以得到结果。但是我的按钮不起作用。以下是我的代码。
URL -
我创建了一个应用程序来分离Python代码 - 应用程序名称是“Python代码”,文件名为“mycode”。
URL -
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的新手。提前谢谢。
{{ output }}
的值将在每个 GET 请求中都存在,而不仅仅是在提交表单后。如果您的 HTML 正确无误,则在</form>
标签上缺少一个闭合角括号。 - Brandon Taylor