Tornado/Python self.render(& quot; example.html& quot;)会忽略CSS [英] Tornado/Python self.render("example.html") ignores CSS

查看:48
本文介绍了Tornado/Python self.render(& quot; example.html& quot;)会忽略CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和一般编程的新手.我使用名为Tornado的Web服务器来托管"我的网站.每当我使用self.render("example.html",此处的变量)生成动态html页面时,所生成的html页面中均未包含CSS,因为在其中生成的html页面仅包含以下内容:即使我将.css文件和example.html页面放在同一文件夹"Tornado/template"中,也没有CSS改善其外观.可以肯定的是,我也正确地将html链接到css标签.

I'm a novice in Python and programming in general. I use a web server called Tornado to "host" my websites. Whenever I use self.render("example.html", variables here) to generate a dynamic html page, the html page produced has no CSS incorporated into it, as in 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.

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

If I open example.html with browser and not with Tornado, it will "render" with the .css file.

由于我不知道为什么会这样,所以我将所有代码发布在这里:这是龙卷风中的app.py:

Since I don't know why this is happening, I'll just post all my code here: This is the app.py in Tornado:

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页面:

And this is the html page that I'm trying to render:

因此,基本上,我从Web用户从不同的html页面接收到两个整数输入,并且在数据库上执行上述app.py的mysql查询.它返回所有结果的列表(我认为这是列表的列表),我使用数据填充下面的html页中的表,但这是包含该表的html页,不会用css呈现"该表

So basically, I receive two integer input from a different html page from a web user and a mysql query is performed the app.py above on a database. It returns a list (I think it's a list of lists) of all the results and I use the data to fill in a table in the html page below but it's the html page containing the table that won't get "rendered" with css.

下部和上部是用户输入(必须为int).question3.html是用于获取用户输入的html页面,而question2.html是具有表的html页面.

lowermass and uppermass is the user inputs (must be int). question3.html is the html page to get the user input and question2.html is the html page with the table.

我真的希望这只是一个愚蠢的错误,我可以很快解决.

I seriously hope it's just a dumb mistake I can quickly fix.

<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>

推荐答案

它仅生成HTML页面,而没有CSS可以改善其外观,即使我已经将我的.css文件和example.html页面放置在同一文件夹中龙卷风/模板".可以肯定的是,我也正确地将html链接到css标签.

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属于您在此处声明的静态文件夹:

The css belongs into the static folder, that you have declared here:

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

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

This is how to link it into the template:

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

这篇关于Tornado/Python self.render(&amp; quot; example.html&amp; quot;)会忽略CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆