Bernie Green
Bernie Green

Reputation: 157

Cannot serve local static CSS file using python package web.py only HTML displays

I am currently taking a python course and when the page loads it will not display with any of the CSS.

I have my Controller.py in the root directory D:\Coding\Python_Projects\Py\CodeWizard

My CSS files are in D:\Coding\Python_Projects\Py\CodeWizard\static\css

My HTML files are in D:\Coding\Python_Projects\Py\CodeWizard\Views\Templates

Controller.py

import web


urls = (
    '/', 'home'
    )

render = web.template.render("Views/Templates", base="MainLayout")
app = web.application(urls, globals())


# Classes/Routes
class home:
    def GET(self):
        return render.home()


if __name__ == "__main__":
    app.run()

MainLayout.html:

$def with (page)
$var css: \\static\css\bootstrap.min.css \\static\css\bootstrap-material-design.min.css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CodeWizard</title>

    $if self.css:
        $for style in self.css.split():
            <link rel="stylesheet" href="$style"/>

</head>
<body>
  <div id="app">
    <nav class="navbar navbar-dark bg-primary fixed-top navbar-expand-lg">
      <div class="navbar-header">
        <a class="navbar-brand">CodeWizard</a>
      </div>

      <div class="nav navbar-nav mr-auto mt-2 mt-lg-0">
        <a class ="nav-item nav-link" href="/">Home Feed</a>
        <a class ="nav-item nav-link" href="/discover">Discover</a>
        <a class ="nav-item nav-link" href="/profile">Profile</a>
        <a class ="nav-item nav-link" href="/settings">Settings</a>
      </div>

      <div class="pull-right">
        <a href"/register" class="btn btn-raised btn-default">Register</a>
      </div>
    </div>
      $:page
  </nav>
</body>
</html>

home.html:

<br /> <br /> <br />

<div class="container">
<h1>Hello CodeWizard</h1>
</div>

If I don't put the extra slash in front of static for the css file location I get a few console errors like StaticApp has no attribute directory and invalid literal for int() with base 10

With the extra slash included I get a 200 OK message. When I inspect the page and look at network it shows for the CSS files a status of cancelled.

Not sure how to fix this. I tried searching quite a bit. I created a shortcut for chrome with

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access --disable-web-security --user-data dir="C:\ChromeLocalFileAccess"

That did not work and I also tried the direct path to the css files along with also trying file:/// and numerous other combinations.

If anyone can help me would be greatly appreciated so I can continue on in the course and learn more python :) Thank you in advance.

Upvotes: 2

Views: 379

Answers (2)

Bahlbi
Bahlbi

Reputation: 25

this works for me so try this in the mainLayout.html

$def with (page)
$var css: static/css/bootstrap.min.css

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>mainlayout</title>
    $if self.css:
        $for style in self.css.split( )
            <link rel ="stylesheet" href="$style" />
</head>
<body>
     <div id="app">
    <nav class="navbar navbar-dark bg-primary fixed-top navbar-expand-lg">
      <div class="navbar-header">
        <a class="navbar-brand">CodeWizard</a>
      </div>

      <div class="nav navbar-nav mr-auto mt-2 mt-lg-0">
        <a class ="nav-item nav-link" href="/">Home Feed</a>
        <a class ="nav-item nav-link" href="/discover">Discover</a>
        <a class ="nav-item nav-link" href="/profile">Profile</a>
        <a class ="nav-item nav-link" href="/settings">Settings</a>
      </div>

      <div class="pull-right">
        <a href="/register" class="btn btn-raised btn-default">Register</a>
      </div>

    </div>
   <div>
       $:page
   </div>


</body>
</html>

Upvotes: 0

tromgy
tromgy

Reputation: 5853

This was an issue with web.py running in Python 3.7 environment. It has been fixed by this pull request.

With that fixed your paths should not start with \\:

either

$var css: static\css\bootstrap.min.css static\css\bootstrap-material-design.min.css

or

$var css: static/css/bootstrap.min.css static/css/bootstrap-material-design.min.css

will work

Upvotes: 1

Related Questions