Tornado/Python self.render(“example.html”)忽略C

2024-04-25 17:52:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python和编程方面是个新手。 我使用一个名为Tornado的网络服务器来“托管”我的网站。每当我使用self.render(“example.html”,这里的变量)生成动态html页面时,生成的html页面没有包含任何CSS,因为它只是生成html页面,没有任何CSS改善其外观,即使我将.CSS文件和example.html页面放在同一个文件夹“Tornado/template”中。我很确定我也得到了链接html到css标签的权利。

如果我用浏览器而不是Tornado打开example.html,它将用.css文件“呈现”。

既然我不知道为什么会这样,我就把我所有的代码贴在这里: 这是龙卷风中的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页面:

因此,基本上,我从一个web用户的不同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>


Tags: pathimportselfwebconfightmlscript页面
1条回答
网友
1楼 · 发布于 2024-04-25 17:52:04

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") }}">

相关问题 更多 >