gpt4 book ai didi

Python 找不到 CSS 文件

转载 作者:行者123 更新时间:2023-11-28 08:29:11 25 4
gpt4 key购买 nike

您好,我刚开始使用 Python 处理 Web 程序,我正在尝试制作一个使用 CSS 样式表的基本网站。我有三个文件:app.py、index.html 和 style.css。它们都在同一个目录中。当我运行 app.py 并转到 localhost:8080 时,它显示“Hello World!”,但它没有来自 style.css 文件的样式,我的终端显示“HTTP/1.1 GET/style.css”- 404 Not Found .但是,当我在 chrome 中打开 index.html 文件而不使用 app.py 文件时,它确实具有样式格式。有什么帮助吗?提前致谢。我的代码如下:

应用.py:

import web

urls = (
'/', 'Index'
)

app = web.application(urls, globals())

web.config.debug = True

class Index(object):

def __init__(self):
self.render = web.template.render('.')

def GET(self):
return self.render.index()

if __name__ == '__main__':
app.run()

index.html:

<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
<title>Basic Web Project</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>

样式.css:

p {
color: green;
font-size: 22px;
}

最佳答案

当您指定 <link type="text/css" rel="stylesheet" href="style.css" /> 时你对你的网络浏览器说了以下的话:style.css 与 index.html 处于同一级别

让我举个例子:

/app
index.html
style.css

如果你从浏览器打开 index.html,你会转到以下 url

file://some/directory/app/index.html

) 与file://some/directory/app/相同因为浏览器在显示网页时会一直搜索索引)当在这个index.html浏览器找到 <link>您指定的那个它会搜索那个 style.css在以下网址下:file://some/directory/app/style.css当然,它会找到该文件。

现在是有趣的部分。

当你打开localhost:8080它会显示localhost:8080/index.html这是有效的,因为你的网络应用程序(你运行的那个 python 脚本)知道当有人访问那个 url 时该怎么做(因为你在 urls 变量中定义它并且你创建了那个 class)

但是当浏览器在此页面上找到 <link>标记他试图去 localhost:8080/style.css你的网络应用程序不知道如何处理,因为你没有在你的应用程序中指定它。

解决方法:您必须为 /style.css 定义一个 url和一个 class将为您呈现该网页。

关于Python 找不到 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28369758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com