我有一个用HTML编写的网页,这些操作是使用JQuery完成的。下面的html代码没有使用任何JQuery,但我想我应该提到这一点,因为我可能需要在其中添加一些AJAX。
这是“前端”
index.html索引
<!DOCTYPE html>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="javaScript.js"></script>
<html>
<div id="liveOutputTitle">
<h1><span>Live Output</span></title>
</div>
<div id="liveOutput">
Value 1 From Python: <input type="text" name="Value1" id="Value1" value="0"><br>
Value 2 From Python: <input type="text" name="Value2" id="Value2" value="0"><br>
Value 3 From Python: <input type="text" name="Value3" id="Value3" value="0"><br>
</div>
我有一个JQuery文件,它不会影响我放在html文件中的任何东西。但这里有一个文件的参考。
jquery.js查询
$(document).ready(function(){
//logic for other functionality of the site
//I'm assuming some AJAX goes here
});
我的最终目标是有一个python程序从某个地方提取值,我希望这些值显示在输入字段中。
我的python“服务器端”代码。做如下事情:
服务器端.py
#!/usr/bin/env python
def getValueOne():
//gets the value from wherever
valueOne = 1
def getValueTwo():
//gets the value from wherever
valueTwo = 2
def getValueThree():
//gets the value from wherever
valueThree = 3
在python中,当这些值改变时,如何将这些值显示为HTML中相应的输入值?
我试着用一些关于AJAX的教程来解决这个问题,但是我还没有把逻辑完全整合起来。我已经读完了this、this和this,但我仍然不确定如何连接这个逻辑。如有任何帮助,我们将不胜感激。
编辑1
我相信有人会说我应该提到我想使用什么框架(假设你必须使用框架)。我不确定,但我读了很多关于烧瓶的书,如果我必须随机选择一个,我会用烧瓶。
最佳答案
您可以尝试在后端构建一个API,然后您将能够从JQuery获取到指定的url并获取一些数据作为响应(通常是json)。
因此,如果将url定义为:http://www.example.com/users
它将返回一个带有用户列表的json。
该url应与后端中的视图关联:
import json
def getUsers():
users = db.get_users() # just an example
return json.dumps(users)
作为一个json,您可以在JQuery中使用它在前端呈现它。