提问人:Dread 提问时间:10/15/2023 更新时间:10/15/2023 访问量:38
在WSGI客户端上将CSS和JSS连接到HTML
Connecting CSS and JSS to HTML on the WSGI client
问:
我有以下代码,用于在女服务员服务器上实现页面路由。我面临着以下任务:我需要连接CSS和JS样式,我该怎么做?
from waitress import serve
def render_template(template_name, context={}):
html_str=""
with open(template_name, 'r') as f:
html_str=f.read()
html_str=html_str.format(**context)
return html_str
def home(environ):
return render_template('templates/index.html', context={})
def contact_us(environ):
return render_template('templates/contact.html', context={})
def contact2(environ):
return render_template('templates/2.html', context={})
def app(environ, start_response):
path= environ.get("PATH_INFO")
if path == "/":
page = home(environ)
elif path == "/contact":
page = contact_us(environ)
elif path == "/contact/2":
page = contact2(environ)
else:
page = render_template('templates/404.html', context={"path":path})
page = page.encode("utf-8")
start_response(
f"200 OK", [
("Content-type", "text/html"),
]
)
return iter([page])
serve(app)
答:
1赞
Ramziya
10/15/2023
#1
通过添加此条件,应用程序会检查请求的路径是否以“/static/”开头。如果是这样,它将尝试直接从指定路径提供静态文件。这样,您的 CSS 和 JS 文件将得到正确提供。
if path.startswith("/static/"):
# Serve static files directly
static_file_path = path.lstrip("/static/")
try:
with open(static_file_path, 'rb') as f:
content = f.read()
start_response("200 OK", [("Content-type", "text/css" if path.endswith(".css") else "text/javascript")])
return [content]
except FileNotFoundError:
start_response("404 Not Found", [("Content-type", "text/plain")])
return [b"404 Not Found"]
评论
0赞
Dread
10/15/2023
请告诉我项目的架构应该是什么?
0赞
Ramziya
10/15/2023
项目结构:my_web_app/ ├── app.py # WSGI应用代码 ├── templates/ # HTML 模板 ├── static/ # 静态文件(CSS 和 JS) ├── server.py # 女服务员服务器配置 └── requirements.txt # 依赖列表
0赞
Dread
10/15/2023
#2
多亏了 Ramziya 的回答,我才想出了该怎么做,非常感谢! 到目前为止,我只连接了CSS样式,但是我已经知道如何用JS来做,我稍后再做!
from waitress import serve
def render_template(template_name, context={}):
html_str=""
with open(template_name, 'r') as f:
html_str=f.read()
html_str=html_str.format(**context)
return html_str
def home(environ):
return render_template('templates/index.html', context={})
def contact_us(environ):
return render_template('templates/contact.html', context={})
def contact2(environ):
return render_template('templates/2.html', context={})
def app(environ, start_response):
path= environ.get("PATH_INFO")
if path.startswith("/src/css/"):
new_path = path.replace('/','',1)
try:
with open(new_path, 'rb') as f:
content = f.read()
start_response("200 OK", [("Content-type", "text/css")])
return [content]
except FileNotFoundError:
start_response("404 Not Found", [("Content-type", "text/plain")])
return [b"404 Not Found css/js"]
if path == "/":
page = home(environ)
elif path == "/contact":
page = contact_us(environ)
elif path == "/contact/2":
page = contact2(environ)
else:
page = render_template('templates/404.html', context={"path":path})
page = page.encode("utf-8")
start_response(
f"200 OK", [
("Content-type", "text/html"),
]
)
return iter([page])
serve(app)
评论