在 Apache 2.2 上运行 Django&mod_wsgi 时提供静态文件服务

发布于 2024-11-16 09:38:29 字数 887 浏览 6 评论 0原文

我已经更改了 Apache 2.2 的 httpd.conf

#Serve static files
Alias /static/ "E:\Python\Django\carlsblog\static\"
<Directory "E:\Python\Django\carlsblog\static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

#Start mod_wsgi as default handler
WSGIScriptAlias / "E:\Python\Django\carlsblog\django.wsgi"

<Directory "E:\Python\Django\carlsblog">
Allow from all
</Directory>

并且我的应用程序可以成功运行,只有静态文件无法访问,Django 抛出 404

但是,如果我将此行更改

Alias /static/ "E:\Python\Django\carlsblog\static\"

Alias /static/ "E:\Python\Django\carlsblog\static"

静态文件夹的索引页可以已看到,但无法访问文件。当我此时检查Apache的错误日志时,我发现了这样的内容:

File does not exist "E:\Python\Django\carlsblog\staticmy_static_file.txt"

似乎少了一个“\”,有人有解决这个问题的想法吗?

I have changed my Apache 2.2's httpd.conf

#Serve static files
Alias /static/ "E:\Python\Django\carlsblog\static\"
<Directory "E:\Python\Django\carlsblog\static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

#Start mod_wsgi as default handler
WSGIScriptAlias / "E:\Python\Django\carlsblog\django.wsgi"

<Directory "E:\Python\Django\carlsblog">
Allow from all
</Directory>

and my app can be successfully run, only the static files cannot be accessed, a 404 is thrown by Django

However, if I change this line

Alias /static/ "E:\Python\Django\carlsblog\static\"

into

Alias /static/ "E:\Python\Django\carlsblog\static"

the index page of the static folder can be seen, but the files cannot be reached. When I checked Apache's error log at this time, I found something like this:

File does not exist "E:\Python\Django\carlsblog\staticmy_static_file.txt"

it seems there's a "\" missing, anyone has an idea on fixing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-11-23 09:38:29

尝试从别名本身中删除尾部斜杠:

Alias /static "E:\Python\Django\carlsblog\static"

更新(2015):这个答案来自2011年。目前,使用gunicorn + nginx来为django应用程序提供服务被认为是更好的做法。

Try removing the trailing slash from the alias itself:

Alias /static "E:\Python\Django\carlsblog\static"

Update (2015): This answer is from 2011. Currently, it is considered a much better practice to use gunicorn + nginx to serve django apps.

若水般的淡然安静女子 2024-11-23 09:38:29

你的模板还好吗?在浏览器中使用View Source 检查生成的 html。

Are your templates OK? Check your generated html using View Source in your browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文