我有一个简单的“投票”网页,其中向用户显示包含三列的表格。每列将包含搜索引擎查询的结果,然后用户将选择哪个列具有更好的结果并单击按钮。
以下是一个示例:http://jsfiddle.net/rfeGa/
我需要以下方面的帮助: 1.如何跟踪html页面和python程序之间的投票? 2.我想在python文件中保留查询列表,如何将该信息传递给网页?
这是我的html页面:
<!DOCTYPE html>
<html>
<head>
<title>Search Engine Comparator!</title>
</head>
<body>
% queries = ("ccny", "bmcc", "qcc");
% bingScore = googScore = yhooScore = 0;
% for q in queries:
The query is: {{ q }}
<br />
And the scores are: Bing = {{ bingScore }}, Google = {{ googScore }}, Yahoo = {{ yhooScore }}.
<hr>
<form action="/" method="post">
<table border="1" width="100%">
<tr>
<th>
<input type="submit" name="engine1" value="Engine 1 is better!" />
</th>
<th>
<input type="submit" name="engine2" value="Engine 2 is better!" />
</th>
<th>
<input type="submit" name="engine3" value="Engine 3 is better!" />
</th>
</tr>
</table>
</form>
% end
</body>
</html>
<script type="text/javascript">
</script>
这是我的python代码:
from bottle import request, route, run, view
from mini_proj import *
@route('/', method=['GET', 'POST'])
@view('index.html')
def index():
return dict(parts = request.forms.sentence.split(),
show_form = request.method == 'GET');
run(host = 'localhost', port = 9988);
答案 0 :(得分:2)
您可以使用jquery + mod_WSGI让Python成为Javascript前端的后端。您可以将结果推送回Python脚本,并让它修改数据库。以下是解决方案的一些简化部分。我遗漏了一些细节。
我不熟悉bottle
,但我过去曾使用web.py
来帮助实现这一目标。
我修改了你的jsfiddle以使用以下jquery中的基本部分。您的javascript将返回单击了哪个按钮。
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("input ").click(function() {
// this captures passes back the value of the button that was clicked.
var input_string = jQuery(this).val();
alert ( "input: " + input_string );
jQuery.ajax({
type: "POST",
url: "/myapp/",
data: {button_choice: input_string},
});
});
});
</script>
让你的python脚本修改后端的数据库。
这里使用web.py来从jQuery获取输入 导入网站 import pyodbc
urls = ( '/', 'broker',)
render = web.template.render('/opt/local/apache2/wsgi-scripts/templates/')
application = web.application(urls, globals()).wsgifunc()
class broker:
def GET(self):
# here is where you will setup search engine queries, fetch the results
# and display them
queries_to_execute = ...
return str(queries_to_execute)
def POST(self):
# the POST will handle values passed back by jQuery AJAX
input_data = web.input()
try:
print "input_data['button_choice'] : ", input_data['button_choice']
button_chosen = input_data['button_choice']
except KeyError:
print 'bad Key'
# update the database with the user input in the variable button_chosen
driver = "{MySQL ODBC 5.1 Driver}"
server = "localhost"
database = "your_database"
table = "your_table"
conn_str = 'DRIVER=%s;SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % ( driver, server, database, uid, pwd )
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
# you need to put some thought into properly update your table to avoid race conditions
update_string = "UPDATE your_table SET count='%s' WHERE search_engine='%s' "
cursor.execute(update_string % (new_value ,which_search_engine)
cnxn.commit()
使用mod_WSGI调用python文件调用。看看如何设置Apache + mod_WSGI + web.py后端,就像我在previous答案中描述的那样。