gpt4 book ai didi

python - Tornado/Python self.render ("example.html") 忽略 CSS

转载 作者:可可西里 更新时间:2023-11-01 13:10:25 26 4
gpt4 key购买 nike

我是 Python 和一般编程的新手。我使用名为 Tornado 的 Web 服务器来“托管”我的网站。每当我使用 self.render("example.html", variables here) 生成动态 html 页面时,生成的 html 页面没有合并 CSS,因为它只是生成 html 页面即使我将 .css 文件与 example.html 页面放在同一文件夹“Tornado/template”中,也没有 CSS 改善其外观。很确定我也正确地获得了 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 页面中的表格,但它是包含不会用 css“呈现”的表格的 html 页面.

lowermass 和 uppermass 是用户输入(必须是整数)。 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>

最佳答案

it just generates the html page with no CSS improving its appearance even though I've placed my .css file along with the example.html page in the same folder "Tornado/template". Pretty sure I got that link html to css tag right as well.

css 属于静态文件夹,您已在此处声明:

"static_path": os.path.join(config.base_dir, "static")

这是将它链接到模板中的方法:

<link rel="stylesheet" href="{{ static_url("presnt.css") }}">

关于python - Tornado/Python self.render ("example.html") 忽略 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23988185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com