在 Django 生产教程中提供静态文件

发布于 2024-11-02 18:12:17 字数 520 浏览 4 评论 0原文

有人有关于在 Django 生产应用程序上提供静态文件的简单分步教程吗?我读了 Django docs ,听起来真的很复杂...我正在尝试使用不同的服务器(如lighttpd、nginx 或cherokee)来提供静态文件,但设置这些对我来说完全是希腊语。我下载了lighttpd,尝试按照说明进行安装,但几秒钟内就出现错误。缺少这个或那个或诸如此类的...我不是一个 UNIX 高手,而且我不太擅长 C/C++,所以所有这些 ./configure 和 MAKE install 对我来说都是胡言乱语...所以我想我直接的问题是:

  1. 您建议使用哪种服务器来提供易于安装和维护的静态文件?
  2. 假设我实际上让服务器启动并运行,然后呢?我如何告诉 Django 在其他服务器上查找文件?
  3. 再说一遍,有人有分步教程吗?

多谢!

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:

  1. Which server would you recommend to serve static files that's easy to install and easy to maintain?
  2. 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?
  3. Again, anyone has step-by-step tutorials?

Thanks a lot!

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

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

发布评论

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

评论(4

绝不放开 2024-11-09 18:12:17

抱歉,没有分步教程。但这里是一个可能有帮助的高级概述:

  1. 您可能想要使用 Apache 服务器( http://httpd.apache .org/)大多数 *nix 发行版都附带此功能。
  2. 然后你想使用 mod python (或者正如评论者指出的 mod_wsgi: http: //docs.djangoproject.com/en/dev/howto/deployment/modwsgi/) 连接到 Django : http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs。完成此步骤后,Apache 现在就领先于 Django。
  3. 接下来,您要将 Django 中的静态文件收集到一个目录中,并将 apache 指向该目录。如果您使用 django.contrib.staticfiles (http://docs.djangoproject.com/en/dev/howto/static-files/),您可以使用 ./manage.pycollectstatic 来完成此操作。

所以诀窍是您'我们没有告诉 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:

  1. You probably want to go with the Apache server ( http://httpd.apache.org/) This comes with most *nix distributions.
  2. You then want to use mod python (or as the commenter pointed out mod_wsgi: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/) to connect to Django : http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs. Once you complete this step, Apache is now fronting for Django.
  3. Next you want to collect the static files in your Django into one directory and point apache at that directory. You can do this using the the ./manage.py collectstatic if you used django.contrib.staticfiles (http://docs.djangoproject.com/en/dev/howto/static-files/.)

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.

别理我 2024-11-09 18:12:17

使用最新的 Django 版本(如 Django 3.2.6)时,我在 DEBUG = False 时在开发和生产环境中提供媒体和静态文件时遇到问题。

因此,我找到了来自多个堆栈溢出帖子的解决方案。

  1. 将适当的函数导入到 urls.py
from django.urls import include, path, re_path
from django.views.static import serve
  1. 将静态 URL 模式列表定义到 urls.py
static_urlpatterns = [
    re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT}),
    re_path(r"^static/(?P<path>.*)$", serve, {"document_root": settings.STATIC_ROOT}),
]

假设您的 STATIC_ROOTMEDIA_ROOT 是已经在 settings.py 文件中定义,

  1. 只需在 urlpatterns 中包含 static_urlpatterns
urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/", include(api_urlpatterns)),
    path("", include(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.

  1. Import appropriate functions to urls.py
from django.urls import include, path, re_path
from django.views.static import serve
  1. Define static URL pattern list to urls.py
static_urlpatterns = [
    re_path(r"^media/(?P<path>.*)
quot;, serve, {"document_root": settings.MEDIA_ROOT}),
    re_path(r"^static/(?P<path>.*)
quot;, serve, {"document_root": settings.STATIC_ROOT}),
]

Assuming your STATIC_ROOT and MEDIA_ROOT is already defined in settings.py file

  1. Just include static_urlpatterns in urlpatterns
urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/", include(api_urlpatterns)),
    path("", include(static_urlpatterns)),
]

Hope it works for you both in the dev and prod environment when DEBUG = FALSE. Thank you.

回心转意 2024-11-09 18:12:17

开发

STATICFILES_DIRS 应该具有所有静态目录,所有静态文件都驻留在其中。

如果您的文件位于本地计算机中,STATIC_URL 应为 /static/,否则请在此处放置基本 URL,例如 http://example.com/

INSTALLED_APPS 应包含 django.contrib.staticfiles

在模板中,加载 staticfiles 模块:

{% load staticfiles %}
<img src='{% static "images/test.png" %}' alt='img' />

Production

添加 STATIC_ROOT ,Django 使用该模块将 STATICFILES_DIRS 中的所有静态文件收集到其中。

收集静态文件:

$ python manage.py collectstatic

添加urls.py路径:

from . import settings

urlpatterns = patterns('',
..
    url(r'^static/(?P<path>.*)

下面列出了更详细的文章:

, 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)

下面列出了更详细的文章:

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 include django.contrib.staticfiles.

In the template, load the staticfiles module:

{% load staticfiles %}
<img src='{% static "images/test.png" %}' alt='img' />

Production

Add STATIC_ROOT that is used by Django to collect all static files from STATICFILES_DIRS to it.

Collect static files:

$ python manage.py collectstatic

Add the path to urls.py:

from . import settings

urlpatterns = patterns('',
..
    url(r'^static/(?P<path>.*)

More detailed articles are listed below:

, 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)

More detailed articles are listed below:

权谋诡计 2024-11-09 18:12:17

更新了 urls.py

url(....) 格式在 Django 3.0.7 的 urls.py 中不再起作用。

你需要这样做:

urls.py

from django.conf import settings # to import static in deployment
from django.conf.urls.static import static # to import static in deployment
....
urlpatterns = [
....

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # to import static in deployment

参考:https://docs.djangoproject.com/en/3.0/howto/static-files/

Updated for urls.py

the url(....) format doesn't work anymore in urls.py for Django 3.0.7.

you need to do then:

urls.py:

from django.conf import settings # to import static in deployment
from django.conf.urls.static import static # to import static in deployment
....
urlpatterns = [
....

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # to import static in deployment

Reference: https://docs.djangoproject.com/en/3.0/howto/static-files/

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