Tornado / Python self.render ("example.html") ignores CSS

I am new to Python and programming in general. I use a web server called Tornado to "host" my sites. Whenever I use self.render ("example.html", variables here) to create a dynamic html page, the created html page does not contain CSS, because it just creates an html page without improving the CSS style, even if I placed his .css file along with the example.html page in the same "Tornado / template" folder. Pretty sure I got the html link for the CSS tag too.

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

Since I don’t know why this is happening, I will just post all my code here: This is the app.py application 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() 

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

So basically, I get two integer inputs from another html page from the web user, and the mysql query is executed on app.py above in the database. It returns a list (I think this is a list of lists) of all the results, and I use the data to populate the table in the html page below, but it is an html page containing a table that will not be "displayed" using css.

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

I seriously hope this is just a dumb mistake that 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> 

+3
source share
1 answer

it just creates an html page without CSS improvement, even if I put my .css file along with the example.html page in the same Tornado / Template folder. Pretty sure I got the html link for the CSS tag too.

css is in the static folder that you specified here:

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

Here's how to associate it with a template:

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

Source: https://habr.com/ru/post/980103/


All Articles