在 Django 生产教程中提供静态文件
有人有关于在 Django 生产应用程序上提供静态文件的简单分步教程吗?我读了 Django docs ,听起来真的很复杂...我正在尝试使用不同的服务器(如lighttpd、nginx 或cherokee)来提供静态文件,但设置这些对我来说完全是希腊语。我下载了lighttpd,尝试按照说明进行安装,但几秒钟内就出现错误。缺少这个或那个或诸如此类的...我不是一个 UNIX 高手,而且我不太擅长 C/C++,所以所有这些 ./configure 和 MAKE install 对我来说都是胡言乱语...所以我想我直接的问题是:
- 您建议使用哪种服务器来提供易于安装和维护的静态文件?
- 假设我实际上让服务器启动并运行,然后呢?我如何告诉 Django 在其他服务器上查找文件?
- 再说一遍,有人有分步教程吗?
多谢!
Does anyone have a simple step-by-step tutorial about serving static files on a Django production app? I read the Django docs and it sounds really complicated... I'm trying to go the route of serving static files using a different server like lighttpd, nginx, or cherokee, but setting these up is all Greek to me. I downloaded lighttpd, tried to follow the instructions to install, and within a few seconds got an error. Missing this or that or whatnot... I'm not a UNIX whiz and I'm not very good at C/C++, so all this ./configure and MAKE install are gibberish to me... So I guess my immediate questions are:
- Which server would you recommend to serve static files that's easy to install and easy to maintain?
- Assuming I actually get the server up and running, then what? How do I tell Django to look for the files on that other server?
- Again, anyone has step-by-step tutorials?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
抱歉,没有分步教程。但这里是一个可能有帮助的高级概述:
所以诀窍是您'我们没有告诉 Django 将提供静态文件的服务委托给特定的服务器。相反,您要告诉 httpd 哪些 url 是通过 Django 提供的,哪些 url 是静态文件。
另一种说法是所有请求都到达 Apache Web 服务器。 Web服务器会根据您在httpd.conf中指定的规则来决定请求是针对静态文件还是针对django生成的动态文件。如果它是静态文件,它将简单地提供该文件。如果请求是动态文件,它将通过 modpython 将请求传递给 Django。
希望有帮助。
Sorry, don't have a step by step tutorial. But here is a high level overview that might help:
So the trick is you're not telling Django to delegate serving static files to a specific server. Rather you're telling httpd which urls are served via Django and what urls are static files.
Another way of saying this is that all requests come to the Apache web server. The webserver, according to the rules you specify in httpd.conf, will decide whether the request is for a static file or whether it is for a dynamic file generated by django. If it for a static file it will simply serve the file. If the request is for a dynamic file it will, via modpython, pass the request to Django.
Hope that helps.
使用最新的 Django 版本(如 Django 3.2.6)时,我在
DEBUG = False
时在开发和生产环境中提供媒体和静态文件时遇到问题。因此,我找到了来自多个堆栈溢出帖子的解决方案。
urls.py
urls.py
假设您的
STATIC_ROOT
和MEDIA_ROOT
是已经在settings.py
文件中定义,urlpatterns
中包含static_urlpatterns
希望它在
DEBUG 时在开发和生产环境中都适用于您= 假
。谢谢。With the latest Django version like Django 3.2.6 I was having issues serving media and static files both in the dev and prod environment while
DEBUG = False
.So I got around a solution that came from multiple stack overflow posts.
urls.py
urls.py
Assuming your
STATIC_ROOT
andMEDIA_ROOT
is already defined insettings.py
filestatic_urlpatterns
inurlpatterns
Hope it works for you both in the dev and prod environment when
DEBUG = FALSE
. Thank you.开发
STATICFILES_DIRS
应该具有所有静态目录,所有静态文件都驻留在其中。如果您的文件位于本地计算机中,
STATIC_URL
应为/static/
,否则请在此处放置基本 URL,例如http://example.com/
。INSTALLED_APPS
应包含django.contrib.staticfiles
。在模板中,加载 staticfiles 模块:
Production
添加
STATIC_ROOT
,Django 使用该模块将STATICFILES_DIRS
中的所有静态文件收集到其中。收集静态文件:
添加urls.py路径:
下面列出了更详细的文章:
http://blog.xjtian.com/post/52685286308/serving-static-files-in-django-more-complicated
http://agiliq.com/blog/2013/03/serving-static-files-in-django/
Development
STATICFILES_DIRS
should have all static directories inside which all static files are resident.STATIC_URL
should be/static/
if your files are in local machine otherwise put the base URL here e.g.http://example.com/
.INSTALLED_APPS
should includedjango.contrib.staticfiles
.In the template, load the staticfiles module:
Production
Add
STATIC_ROOT
that is used by Django to collect all static files fromSTATICFILES_DIRS
to it.Collect static files:
Add the path to urls.py:
More detailed articles are listed below:
http://blog.xjtian.com/post/52685286308/serving-static-files-in-django-more-complicated
http://agiliq.com/blog/2013/03/serving-static-files-in-django/
更新了 urls.py,
url(....)
格式在 Django 3.0.7 的urls.py
中不再起作用。你需要这样做:
urls.py
:参考:https://docs.djangoproject.com/en/3.0/howto/static-files/
Updated for urls.py
the
url(....)
format doesn't work anymore inurls.py
for Django 3.0.7.you need to do then:
urls.py
:Reference: https://docs.djangoproject.com/en/3.0/howto/static-files/