Jinja not rendering css/images in sub-directories

核能气质少年 提交于 2019-12-22 14:48:10

问题


I'm using Jinja2 and GAE, and I'm using a helper function to render HTML files. When I try to render a HTML file that is nested within a subdirectory (Gamification), Jinja doesn't seem to be pulling out the CSS and images files. Does anyone know what I should do?

My current YAML structure is as follows

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.1"
- name: jinja2
  version: latest

My current directory structure is as such.

Main directory
|
 -- main.py

|
 ----Templates
     |
      --Gamification
        |
         --index.html
           |
            --CSS
              |
               --1.css
           |
            --img
              |
               --1.jpef

main.py

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(['Templates', 'Templates\Gamification']));
def import_html(self, address, val = {}):
    template = jinja_environment.get_template(address);
    self.write(template.render(val));

The html file in question is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Gamification</title>
    <link href="css/bootstrap.css" rel="stylesheet">
  </head>

  <body>

<img src = "img/dan.jpeg" class = "img-rounded" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>

回答1:


It looks like you didn't declare static directories for the img and css.
something like:

- url: /css
  static_dir: css



回答2:


Yes, I had the same problem. You need to do something like this:

- url: /.*/css
  static_dir: templates/css

- url: /.*/js
  static_dir: templates/js

(of course, replace "templates" with whatever subdirectory your css and js folders are located)



来源:https://stackoverflow.com/questions/12434187/jinja-not-rendering-css-images-in-sub-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!