python - 使用 jinja2 在 python web 应用程序中加载 css

标签 python html css jinja2 wsgi

我正在尝试制作一个小型生物信息学 Web 应用程序。我让它与 CGI 一起工作,我正在尝试对其进行调整,以便我可以在 pythonanywhere 或 heroku 上提供它。

我遇到的问题是样式表和 javascript 没有加载。我对编程很陌生,但在发布之前我已经进行了广泛的搜索。我从应用程序中删除了很多重量以隔离问题。该应用程序的目录现在如下所示:

/app
 main.html
 main.css
 main.js
 WSGI_app.py

WSGI_app.py 中的代码如下所示:

from wsgiref.simple_server import make_server
import jinja2

def application( environ, start_response ):
    templateLoader = jinja2.FileSystemLoader( searchpath="./" )
    env = jinja2.Environment( loader=templateLoader )
    template = env.get_template( 'main.html' )
    template = template.render()
    start_response("200 OK", [( "Content-Type", "text/html" )])
    yield(template.encode( "utf8" ))

httpd = make_server('localhost', 8051, application)
httpd.serve_forever()

main.html 的顶部如下所示:

<!doctype html>
<html lang="en">
 <head>
    <meta charset="utf-8" />
    <title>Chimera Quest</title>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <link type="text/css" rel="stylesheet" href="./main.css" />
    <script type="text/javascript" src="./main.js"></script>
</head>

我试过用“/main.css”和“main.css”替换“./main.css”。

我浏览器的控制台显示:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8051/main.css".

有没有人看过这里?我完全没有想法。

最佳答案

如果您使用的 WSGI 服务器还不支持托管静态文件,例如 Apache 的 mod_wsgi,那么最好的办法是使用 WSGI 中间件来处理提供静态文件的服务。人们使用的主要此类 WSGI 中间件称为 Whitenoise。

关于python - 使用 jinja2 在 python web 应用程序中加载 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137499/

相关文章:

python - 混合视频流最佳实践

javascript - CSS 在特定点更改背景颜色

jquery - 单击而不刷新页面后关闭悬停子菜单

javascript - 如何将标签的宽度设置为在 Canvas 上的图表上居中?

javascript - 带有选择类的语义 UI 多级下拉列表不会展开

python - 在Python中删除重复的键并附加已删除键的值

python - 仅当字符串匹配时才在 python 中连接字符串

javascript - 通过更改不同行中选项的值来选中/取消选中复选框

html - 表格的 CSS 问题

python - 类型错误 : src data type = 17 is not supported