Search code examples
pythoncssstyleswsgi

A simple wsgi website with python, but the .css file does not load. Why?


Why is my background not blue when I go to localhost:8080 in my browser? The following 3 files are all located in the same directory:

wsgiwebsite.py

#!/usr/bin/env python
from wsgiref.simple_server import make_server

cont = (open('wsgiwebsite_content.html').read())

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [cont]

server = make_server('0.0.0.0', 8080, application)
server.serve_forever()

wsgiwebsite_content.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="wsgiwebsite_style.css">
  </head>

  <body>
    This is my first web page
  </body>
</html>

wsgiwebsite_style.css

body{background-color:blue;}

Solution

  • WSGI only serves your Python code and probably doesn't even know about existence of that CSS file.

    You can either configure your web-server to handle static assets for you, or use something like static to also serve static media.