Django is recommending me that if I am going to only use one server (Apache) to serve both dynamic and static files, then I should serve static files using django.contrib.staticfiles
.
Django建议我,如果我只使用一台服务器(Apache)来提供动态和静态文件,那么我应该使用django.contrib.staticfiles提供静态文件。
So in my settings.py
I have loaded django.contrib.staticfiles
to my INSTALLED_APPS
and django.core.context_processors.static
to my TEMPLATE_CONTEXT_PROCESSORS
.
所以在我的settings.py中,我已经将django.contrib.staticfiles加载到我的INSTALLED_APPS,并将django.core.context_processors.static加载到我的TEMPLATE_CONTEXT_PROCESSORS。
I noticed in the admin templates that it links to static files like this (from index.html
):
我注意到在管理模板中它链接到这样的静态文件(来自index.html):
{% load i18n admin_static %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}" />{% endblock %}
But looking at the template tag admin_static
, it's simply a wrapper for static
:
但是看一下模板标签admin_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)
So I concluded that because every admin static file is serverd with a admin/...
prefix, then the full path (for my case) should be
所以我得出结论,因为每个管理静态文件都是带有admin / ...前缀的服务器,那么完整路径(对于我的情况)应该是
/usr/lib64/python2.7/site-packages/django/contrib/admin/static
So I set that path to my STATICFILES_DIRS
inside settings.py
, but Apache still won't serve any static files (after restating the server). Where did I make a mistake in my logic?
所以我将该路径设置为settings.py中的STATICFILES_DIRS,但Apache仍然不会提供任何静态文件(在重新启动服务器之后)。我的逻辑在哪里犯了错误?
2 个解决方案
#1
9
Thanks Daniel Roseman for the explanation and giving me the chance to learn it on my own (and now I won't forget!) :-).
感谢Daniel Roseman的解释并让我有机会自己学习(现在我不会忘记!):-)。
Initially I was really confused and I didn't know you had to first collect the static files, then tell Apache to serve it. I thought simply using STATICFILES_DIRS
and including the static
app in settings.py
was good enough.
最初我真的很困惑,我不知道你必须先收集静态文件,然后告诉Apache服务它。我想只需使用STATICFILES_DIRS并在settings.py中包含静态应用就足够了。
So here is how I did it (and please let me know if I could have done it better):
所以这就是我如何做到的(如果我能做得更好,请告诉我):
In settings.py
STATIC_ROOT = '/var/www/localhost/htdocs/mysite/static/'
STATIC_URL = '/static/' # default
It seems Django already know where to collect the admin files, you don't need to specify anything in STATICFILES_DIRS
unless you need to serve your own custom files (which I don't and thus I had no prior experience with static files in Django).
似乎Django已经知道在哪里收集管理文件,你不需要在STATICFILES_DIRS中指定任何东西,除非你需要提供自己的自定义文件(我没有,因此我之前没有使用Django中的静态文件) 。
Then at /var/www/localhost/htdocs/mysite/
type python manage.py collectstatic -l
. The -l
means to create a symbolic link to all found static files instead of copying it over (saves some space).
然后在/ var / www / localhost / htdocs / mysite / type python manage.py collectstatic -l。 -l表示创建指向所有找到的静态文件的符号链接,而不是将其复制(节省一些空间)。
Next edit the Apache config file (usually httpd.conf
) and add the STATIC_URL
information. My config file just for Django looks like this:
接下来编辑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>
Then restart Apache and done!
然后重启Apache并完成!
#2
3
The documentation you link to doesn't say anything at all about serving files with the staticfiles app. That's not what it's for: it's for collecting staticfiles into a single place to allow them to be easily served by Apache. (It does deal with serving files in development, but that's not what we're talking about here.)
您链接到的文档没有说明使用staticfiles应用程序提供文件。这不是它的用途:它用于将静态文件收集到一个地方,以便Apache可以轻松地为它们提供服务。 (它确实处理开发中的文件服务,但这不是我们在这里讨论的内容。)
You still need to set up Apache to serve the files from the relevant location via the static/ prefix.
您仍然需要设置Apache以通过静态/前缀从相关位置提供文件。
#1
9
Thanks Daniel Roseman for the explanation and giving me the chance to learn it on my own (and now I won't forget!) :-).
感谢Daniel Roseman的解释并让我有机会自己学习(现在我不会忘记!):-)。
Initially I was really confused and I didn't know you had to first collect the static files, then tell Apache to serve it. I thought simply using STATICFILES_DIRS
and including the static
app in settings.py
was good enough.
最初我真的很困惑,我不知道你必须先收集静态文件,然后告诉Apache服务它。我想只需使用STATICFILES_DIRS并在settings.py中包含静态应用就足够了。
So here is how I did it (and please let me know if I could have done it better):
所以这就是我如何做到的(如果我能做得更好,请告诉我):
In settings.py
STATIC_ROOT = '/var/www/localhost/htdocs/mysite/static/'
STATIC_URL = '/static/' # default
It seems Django already know where to collect the admin files, you don't need to specify anything in STATICFILES_DIRS
unless you need to serve your own custom files (which I don't and thus I had no prior experience with static files in Django).
似乎Django已经知道在哪里收集管理文件,你不需要在STATICFILES_DIRS中指定任何东西,除非你需要提供自己的自定义文件(我没有,因此我之前没有使用Django中的静态文件) 。
Then at /var/www/localhost/htdocs/mysite/
type python manage.py collectstatic -l
. The -l
means to create a symbolic link to all found static files instead of copying it over (saves some space).
然后在/ var / www / localhost / htdocs / mysite / type python manage.py collectstatic -l。 -l表示创建指向所有找到的静态文件的符号链接,而不是将其复制(节省一些空间)。
Next edit the Apache config file (usually httpd.conf
) and add the STATIC_URL
information. My config file just for Django looks like this:
接下来编辑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>
Then restart Apache and done!
然后重启Apache并完成!
#2
3
The documentation you link to doesn't say anything at all about serving files with the staticfiles app. That's not what it's for: it's for collecting staticfiles into a single place to allow them to be easily served by Apache. (It does deal with serving files in development, but that's not what we're talking about here.)
您链接到的文档没有说明使用staticfiles应用程序提供文件。这不是它的用途:它用于将静态文件收集到一个地方,以便Apache可以轻松地为它们提供服务。 (它确实处理开发中的文件服务,但这不是我们在这里讨论的内容。)
You still need to set up Apache to serve the files from the relevant location via the static/ prefix.
您仍然需要设置Apache以通过静态/前缀从相关位置提供文件。