为什么在我的CSS中提供网页字体文件时会出现500错误?

1 投票
2 回答
2374 浏览
提问于 2025-04-18 04:17

我正在开始一个Django应用,目前只是想先展示一些静态页面。现在一切看起来都正常,但我在加载一些网页字体时,CSS文件出现了500的HTTP响应。

我在引用我的CSS文件时使用了:

<link rel="stylesheet" type="text/css" href="{% static "css/styles.css" %}">

这个引用没问题,但在CSS里面有一些链接指向网页字体,这些链接是按照FontSquirrel生成的标准格式来的。例如:

@font-face {
  font-family: 'allerbold';
  src: url("../fonts/aller_bd-webfont.eot");
  src: url("../fonts/aller_bd-webfont.eot?#iefix") format("embedded-opentype"), url("../fonts/aller_bd-webfont.woff") format("woff"), url("../fonts/aller_bd-webfont.ttf") format("truetype"), url("../fonts/aller_bd-webfont.svg#allerbold") format("svg");
  font-weight: normal;
  font-style: normal; }

CSS文件确实指向了正确的文件夹('css'和'fonts'文件夹在静态文件夹里是同一级别的),但是终端和浏览器控制台(Chrome)都显示了HTTP 500的响应。

这是其中一个错误的追踪信息:

Traceback (most recent call last):
  File "C:\Server\Python34\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Server\Python34\lib\site-packages\django\contrib\staticfiles\handlers
.py", line 68, in __call__
    return super(StaticFilesHandler, self).__call__(environ, start_response)
  File "C:\Server\Python34\lib\site-packages\django\core\handlers\wsgi.py", line
 206, in __call__
    response = self.get_response(request)
  File "C:\Server\Python34\lib\site-packages\django\contrib\staticfiles\handlers
.py", line 58, in get_response
    return self.serve(request)
  File "C:\Server\Python34\lib\site-packages\django\contrib\staticfiles\handlers
.py", line 51, in serve
    return serve(request, self.file_path(request.path), insecure=True)
  File "C:\Server\Python34\lib\site-packages\django\contrib\staticfiles\views.py
", line 41, in serve
    return static.serve(request, path, document_root=document_root, **kwargs)
  File "C:\Server\Python34\lib\site-packages\django\views\static.py", line 65, i
n serve
    response["Last-Modified"] = http_date(statobj.st_mtime)
  File "C:\Server\Python34\lib\site-packages\django\utils\http.py", line 109, in
 http_date
    rfcdate = formatdate(epoch_seconds)
  File "C:\Server\Python34\lib\email\utils.py", line 181, in formatdate
    now = time.gmtime(timeval)
OSError: [Errno 22] Invalid argument
[25/Apr/2014 13:19:09] "GET /static/fonts/aller_bd-webfont.svg HTTP/1.1" 500 59

谢谢大家的帮助!

2 个回答

1

虽然我来得有点晚,但对于像我一样在网上搜索的朋友们,分享一下我的发现:

试着去掉'../'。在我的情况下,网页字体没有问题,但浏览器在处理“同源策略”时遇到了麻烦(更多信息可以查看MDN)。简单来说,使用'../'去找字体时,浏览器不总是认为这是同一个来源,所以去掉它就能解决问题。

0

经过一番尝试和错误,我发现问题出在网页字体文件本身。我注意到一组不是从FontSquirrel生成的网页字体可以正常使用,于是我尝试用另一个工具(www.web-font-generator.com)转换其他字体,现在它们都能正常加载了。

为了验证这个发现,我用这两个生成工具转换了另一个测试字体,果然,FontSquirrel生成的那个不行,而Web-Font-Generator生成的可以正常使用。

真是奇怪!

撰写回答