【问题标题】:How to serve admin static files with django.contrib.staticfiles in Django 1.4 (using one Apache server)?如何在 Django 1.4 中使用 django.contrib.staticfiles 提供管理静态文件(使用一台 Apache 服务器)?
【发布时间】:2012-04-15 03:16:10
【问题描述】:

Django 建议我,如果我打算只使用一台服务器 (Apache) 来同时提供动态和静态文件,那么 I should serve static files using django.contrib.staticfiles

所以在我的settings.py 中,我已将django.contrib.staticfiles 加载到我的INSTALLED_APPSdjango.core.context_processors.static 到我的TEMPLATE_CONTEXT_PROCESSORS

我在管理模板中注意到它链接到这样的静态文件(来自index.html):

{% load i18n admin_static %}

{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}" />{% endblock %}

但是看看模板标签admin_static,它只是static的一个包装器:

from django.conf import settings
from django.template import Library

register = Library()

if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
    from django.contrib.staticfiles.templatetags.staticfiles import static
else:
    from django.templatetags.static import static

static = register.simple_tag(static)

所以我得出的结论是,因为每个管理静态文件都使用 admin/... 前缀作为服务器,那么(就我而言)完整路径应该是

/usr/lib64/python2.7/site-packages/django/contrib/admin/static

所以我在settings.py 中将路径设置为我的STATICFILES_DIRS,但Apache 仍然不会提供任何静态文件(在重新启动服务器之后)。我的逻辑哪里出错了?

【问题讨论】:

    标签: python django apache django-templates django-staticfiles


    【解决方案1】:

    感谢 Daniel Roseman 的解释,让我有机会自学(现在我不会忘记了!):-)。

    一开始我真的很困惑,我不知道你必须先收集静态文件,然后告诉Apache 服务它。我认为只需使用STATICFILES_DIRS 并在settings.py 中包含static 应用程序就足够了。

    这就是我的做法(如果我可以做得更好,请告诉我):

    settings.py

    STATIC_ROOT = '/var/www/localhost/htdocs/mysite/static/'
    STATIC_URL = '/static/' # default
    

    似乎 Django 已经知道在哪里收集管理文件,您不需要在 STATICFILES_DIRS 中指定任何内容,除非您需要提供自己的自定义文件(我不知道,因此我之前没有经验Django 中的静态文件)。

    然后在/var/www/localhost/htdocs/mysite/ 输入python manage.py collectstatic -l-l 意味着创建一个指向所有找到的静态文件的符号链接,而不是复制它(节省一些空间)。

    接下来编辑 Apache 配置文件(通常是 httpd.conf)并添加 STATIC_URL 信息。我的 Django 配置文件如下所示:

    Alias /static/ /var/www/localhost/htdocs/mysite/static/
    #In the form of...
    #Alias STATIC_URL STATIC_ROOT
    
    <Directory /var/www/localhost/htdocs/mysite/static>
        Order deny,allow
        Allow from all
    </Directory>
    
    WSGIScriptAlias / /var/www/localhost/htdocs/mysite/mysite/wsgi.py
    WSGIPythonPath /var/www/localhost/htdocs/mysite
    
    <Directory /var/www/localhost/htdocs/mysite/mysite>
        <Files wsgi.py>
            Order deny,allow
            Allow from all
        </Files>
    </Directory>
    

    然后重启Apache就完成了!

    【讨论】:

      【解决方案2】:

      您链接到的文档根本没有说明使用 staticfiles 应用程序服务文件。这不是它的用途:它是为了将静态文件收集到一个地方,以便让 Apache 轻松地为它们提供服务。 (它确实处理在开发中提供文件,但这不是我们在这里讨论的内容。)

      您仍然需要设置 Apache 以通过 static/ 前缀从相关位置提供文件。

      【讨论】:

      • 我已经设置了 Apache 以使用 mod_wsgi 和使用 Django 1.4 中包含的 wsgi.py 部署 Django。我关注了documentation's suggestion
      • 所以?您仍然需要对其进行设置以提供静态文件,如该页面下方所述。
      • 那我就糊涂了。我需要去/var/www/localhost/htdocs/ 并创建一个static/usr/lib64/python2.7/site-packages/django/contrib/admin/static 的符号链接吗?
      • 我认为使用 django.contrib.staticfiles 是 4 个可用选项之一?其他 3 个选项是“创建符号链接...”、“使用别名指令...”和“复制管理静态文件...”。
      • 否,该部分中的“使用静态文件”解释为“使用 collectstatic 管理命令收集 STATIC_ROOT 中的静态文件,然后将您的 Web 服务器配置为在 STATIC_URL 提供 STATIC_ROOT”。
      猜你喜欢
      • 1970-01-01
      • 2012-09-21
      • 2012-03-19
      • 2014-10-08
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2017-02-21
      相关资源
      最近更新 更多