我是 Python 和一般编程的新手。
我使用名为 Tornado 的网络服务器来“托管”我的网站。每当我使用 self.render("example.html",这里的变量)生成动态 html 页面,生成的 html 页面没有合并任何 CSS,因为它只是生成 html 页面,没有 CSS 改善其外观,即使我将 .css 文件与 example.html 页面一起放置在同一文件夹“龙卷风/模板”中。很确定我也正确获取了 html 到 css 标签的链接。
如果我使用浏览器而不是 Tornado 打开 example.html,它将使用 .css 文件“渲染”。
由于我不知道为什么会发生这种情况,因此我将在这里发布所有代码:
这是 Tornado 中的 app.py:
import config
import os.path
import re
import MySQLdb
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("question3.html")
class StopTornado(tornado.web.RequestHandler):
def get(self):
tornado.ioloop.IOLoop.instance().stop()
class ReturnQuery(tornado.web.RequestHandler):
def post(self):
connection = MySQLdb.connect(**config.mysql_config)
cursor = connection.cursor()
if 'lowermass' in self.request.arguments and 'uppermass' in self.request.arguments:
lowermass = self.get_argument('lowermass')
uppermass = self.get_argument('uppermass')
# Testing for bad input and should block Injection attacks
# Since you can't really do an injection attack with only numbers
# Placing any non-int input will throw an exception and kick you to the Error.html page
try:
lowermass = int(lowermass)
uppermass = int(uppermass)
except ValueError:
self.render("Error.html")
if lowermass < uppermass:
cursor.execute ('''SET @row_num=0;''')
cursor.execute('''SELECT @row_num:=@row_num+1 as 'Num', b.commonname
FROM Bird b
JOIN Bodymass m ON b.EOLid = m.EOLid
WHERE m.mass BETWEEN %s AND %s
GROUP BY b.commonname''',(lowermass, uppermass))
birds = cursor.fetchall()
self.render("question2.html", birds = birds)
else:
self.render("Error.html")
else :
self.render("Error.html")
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
# Add more paths here
(r"/KillTornado/", StopTornado),
(r"/tables/", ReturnQuery),
(r"/tables/localhost8888", MainHandler)
]
settings = {
"debug": True,
"template_path": os.path.join(config.base_dir, "templates"),
"static_path": os.path.join(config.base_dir, "static")
}
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == "__main__":
app = Application()
app.listen(config.port)
print "Starting tornado server on port %d" % (config.port)
tornado.ioloop.IOLoop.instance().start()
这是我试图呈现的 html 页面:
所以基本上,我从网络用户的不同 html 页面收到两个整数输入,并在数据库上执行上面的 app.py 的 mysql 查询。它返回所有结果的列表(我认为这是列表的列表),我使用数据填充下面的 html 页面中的表格,但它是包含表格的 html 页面,不会用 css“渲染” 。
lowermass 和 uppermass 是用户输入(必须是 int)。 Question3.html 是获取用户输入的 html 页面,question2.html 是带有表格的 html 页面。
我真心希望这只是一个愚蠢的错误,我可以很快修复。
<html>
<head>
<link rel = "stylesheet" type ="text/css" href = "presnt.css">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function(){
$('#table').dataTable();
});
</script>
<title>Birds with body mass in range</title>
<div id = "header">
Birds with body mass in range
</div>
<br>
</head>
<body>
<table id = "table">
<thead>
<tr>
<td style="padding:4px;border-top:1px solid black;">rownum</td>
<td style="padding:4px;border-top:1px solid black;">common name</td>
</tr>
<tr>
<td style = "padding:1px;border-top:1px solid black;">
</td>
<td style = "padding:1px;border-top:1px solid black;">
</td>
</tr>
</thead>
<tbody>
{% if birds %}
{% for bird in birds %}
<tr>
<td>{{ bird[0] }} </td>
<td>{{ bird[1] }}</td>
</tr>
{% end %}
{% else %}
<tr>
<td colspan = 2> No results returned</td>
</tr>
{% end %}
</tbody>
</table>
</body>