我有下面的代码在waitress服务器上实现页面路由。我面临着以下任务:我需要连接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)
2条答案
按热度按时间kiayqfof1#
通过添加此条件,应用程序会检查请求的路径是否以“/static/"开头。如果是,则尝试直接从指定路径提供静态文件。这样,您的CSS和JS文件将得到正确的服务。
taor4pac2#
感谢Ramziya的回答,我想出了如何做到这一点,为此非常感谢!到目前为止,我只连接了CSS样式,但我已经知道如何用JS来做,我稍后会做!