我正在尝试制作一个小型生物信息学 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/