我看过有关此问题的几个帖子,但没有找到我的解决方案。
我正在尝试在 Django 1.3 开发环境中提供静态文件。
这是我的设置
...
STATIC_ROOT = '/home/glide/Documents/django/cbox/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/static/',
)
...
我的 urls.py
urlpatterns = patterns('',
...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
...
);
我的 /home/glide/Documents/django/cbox/static/ 目录就像
css
main.css
javascript
image
我在尝试访问 http://127.0 时收到 404 错误。 0.1:8000/static/css/main.css。
我是否必须分别指定 css、javascript 和图像的模式?
I've seen several posts for this issue but didn't found my solution.
I'm trying to serve static files within my Django 1.3 development environment.
Here are my settings
...
STATIC_ROOT = '/home/glide/Documents/django/cbox/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/static/',
)
...
My urls.py
urlpatterns = patterns('',
...
url(r'^static/(?P<path>.*)
My /home/glide/Documents/django/cbox/static/ directory is like
css
main.css
javascript
image
I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.
Do I have to specify patterns for css, javascript and images individually ?
, 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
...
);
My /home/glide/Documents/django/cbox/static/ directory is like
I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.
Do I have to specify patterns for css, javascript and images individually ?
发布评论
评论(16)
我混淆了STATIC_ROOT和STATICFILES_DIRS
实际上我并没有真正理解STATIC_ROOT的用途。我认为这是我必须放置常用文件的目录。该目录用于生产,这是 收集静态。
STATICFILES_DIRS 是我需要的。
由于我处于开发环境中,因此我的解决方案是不使用 STATIC_ROOT (或指定其他路径)并在 STATICFILES_DIRS 中设置我的公共文件目录:
也不要不要忘记
从 django.conf 导入设置
I confused STATIC_ROOT and STATICFILES_DIRS
Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.
STATICFILES_DIRS is the one that I need.
Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:
Also don't forget to
from django.conf import settings
settings.py
中可能只有两件事会给您带来问题。1)
STATIC_URL = '/static/'
2)
并且您的静态文件应位于 static 目录下,该目录与项目的设置文件位于同一目录中。
即使如此,如果您的静态文件未加载,那么原因是,您可能一直将
其更改为 True (严格仅用于开发)。在生产中,只需将 STATICFILES_DIRS 更改为静态文件所在的任何路径即可。
There could be only two things in
settings.py
which causes problems for you.1)
STATIC_URL = '/static/'
2)
and your static files should lie under static directory which is in same directory as project's settings file.
Even then if your static files are not loading then reason is , you might have kept
change it to True (strictly for development only). In production just change
STATICFILES_DIRS
to whatever path where static files resides.提供静态文件可以通过多种方式实现;这是我对自己的注释:
static/my_app/
目录添加到my_app
(请参阅下面有关命名空间的注释)STATICFILES_DIRS 设置不应包含 STATIC_ROOT 设置
)我更喜欢第一种方式,以及接近 在文档中定义,因此为了提供文件
admin-custom.css
来覆盖几个对于管理样式,我有一个像这样的设置:然后在模板中使用它,如下所示:
部署时,我运行
collectstatic
并使用 nginx 提供静态文件。这些文档为我消除了所有困惑:
Serving static files can be achieved in several ways; here are my notes to self:
static/my_app/
directory tomy_app
(see the note about namespacing below)The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
)I prefer the first way, and a setup that's close to the way defined in the documentation, so in order to serve the file
admin-custom.css
to override a couple of admin styles, I have a setup like so:This is then used in the template like so:
When deploying, I run
collectstatic
and serve static files with nginx.The docs which cleared up all the confusion for me:
如果您的静态 URL 正确但仍然:
也许是您的 WSGI 问题。
➡配置 WSGI 同时服务于开发环境和生产环境
If your static URL is correct but still:
Perhaps your WSGI problem.
➡ Config WSGI serves both development env and production env
STATIC_ROOT
行static
文件夹。假设目录是:project\static
现在更新:
无论您做什么,要点是
STATICFILES_DIRS
和STATIC_ROOT
不应包含相同的目录。我知道已经过去很长时间了,但希望新朋友能从中得到帮助
STATIC_ROOT
linestatic
folder in different directory. For suppose the directory is:project\static
Now update:
Whatever you do the main point is
STATICFILES_DIRS
andSTATIC_ROOT
should not contain same directory.I know it's been a long time but hope the new buddies can get help from it
STATICFILES_DIRS
用于开发,STATIC_ROOT
用于生产,STATICFILES_DIRS
和STATIC_ROOT
不应具有相同的文件夹名称,如果您需要在开发和生产中使用完全相同的静态文件夹,请尝试此方法,
将其包含在
settings.py
中STATICFILES_DIRS
is used in development andSTATIC_ROOT
in production,STATICFILES_DIRS
andSTATIC_ROOT
should not have same folder name,If you need to use the exact same static folder in development and production, try this method
include this in
settings.py
另一个错误可能是您的应用没有列在
INSTALLED_APPS
列表中,例如:如果没有将其列在其中,您可能会面临诸如未检测到静态文件(基本上是涉及您的应用的所有文件)之类的问题。即使它可以按照正确答案中的建议是正确的,方法是:
可能是错误之一,如果出现此错误,应进行审查。
Another error can be not having your app listed in the
INSTALLED_APPS
listing like:Without having it in, you can face problems like not detecting your static files, basically all the files involving your app. Even though it can be correct as suggested in the correct answer by using:
Can be one of the errors and should be reviewed if getting this error.
在你的cmd中输入命令
python manage.py findstatic --verbosity 2 static
它将给出 Django 在其中查找静态文件的目录。如果您创建了虚拟环境,那么此 virtual_environment_name 文件夹中将会有一个 static 文件夹。
VIRTUAL_ENVIRONMENT_NAME\Lib\site-packages\django\contrib\admin\static
。在运行上面的“findstatic”命令时,如果 Django 显示此路径,则只需将所有静态文件粘贴到此静态目录中。
在您的 html 文件中,使用 JINJA 语法作为 href 并检查其他内联 css。如果在给出 JINJA 语法后仍然存在图像 src 或 url,则在其前面加上 '/static'。
这对我有用。
In your cmd type command
python manage.py findstatic --verbosity 2 static
It will give the directory in which Django is looking for static files.If you have created a virtual environment then there will be a static folder inside this virtual_environment_name folder.
VIRTUAL_ENVIRONMENT_NAME\Lib\site-packages\django\contrib\admin\static
.On running the above 'findstatic' command if Django shows you this path then just paste all your static files in this static directory.
In your html file use JINJA syntax for href and check for other inline css. If still there is an image src or url after giving JINJA syntax then prepend it with '/static'.
This worked for me.
我通过在 INSTALLED_APPS 中添加我的项目名称来解决此问题。
I solve this problem by adding my project name in INSTALLED_APPS.
始终记住 Django 文件末尾的两件事
settings.py
用于开发
用于生产
由于您在开发环境中工作,请注释掉
settings.py
中的其他两个路径> 文件并对
urls.py
进行如下更改希望这可以解决问题
Always remember two things in Django at the end of file
settings.py
For development
For production
Since you are working in a development environment, comment out the other two paths from
settings.py
fileAnd for the
urls.py
make changes as followsHope this may solve the problems
Django 没有用于提供静态文件的内置解决方案,至少在 DEBUG 必须为 False 的生产中是这样。
我们必须使用第三方解决方案来完成此任务。
要在虚拟环境中安装 WhiteNoise,请键入以下命令:
然后修改设置
要让 Django 知道您想要运行 WhitNoise,您必须在 settings.py 文件的 MIDDLEWARE 列表中指定它:
Django does not have a built-in solution for serving static files, at least not in production when DEBUG has to be False.
We have to use a third-party solution to accomplish this.
To install WhiteNoise in your virtual environment, type the command below:
then Modify Settings
To make Django aware of you wanting to run WhitNoise, you have to specify it in the MIDDLEWARE list in settings.py file:
如果您在开发环境中,则需要定义
STATIC_URL
和STATICFILES_DIR
;如果您在生产环境中,则需要定义STATIC_URL
和>STATIC_ROOT
对于 Windows:
以及 Linux 环境
If you are on development environment you will need to define
STATIC_URL
andSTATICFILES_DIR
and if you are on production environment you need to defineSTATIC_URL
andSTATIC_ROOT
For windows:
And for linux environment
我发现我将本地设置中的
DEBUG
设置移动为被默认False
值覆盖。如果您使用DEBUG
和runserver
进行开发,本质上要确保 DEBUG 设置实际上为 false。I found that I moved my
DEBUG
setting in my local settings to be overwritten by a defaultFalse
value. Essentially look to make sure the DEBUG setting is actually false if you are developing withDEBUG
andrunserver
.如果您添加了
django-storages
模块(例如,支持在 django 应用程序中将文件上传到 S3),并且像我一样您没有正确阅读 此模块的文档,只需从settings.py 中删除此行
:否则,它将导致您的静态资产不在本地计算机上查看,而是在 S3 存储桶中远程查看,包括管理面板 CSS,从而有效地破坏管理面板 CSS。
If you've have added the
django-storages
module (to support uploading files to S3 in your django app for instance), and if like me you did not read correctly the documentation of this module, just remove this line from yoursettings.py
:Otherwise it will cause your static assets to be looked NOT on your local machine but remotely in the S3 bucket, including admin panel CSS, and thus effectively breaking admin panel CSS.
您只需要用于开发:
用于生产(使用 /manage.pycollectstatic 时):
在 Debug = False 之后,您的静态文件将不会加载。您可以通过输入 127.0.0.1:8000/staticfiles/css/main.css (例如)来检查它。
因此,您需要更改为 Debug = True,然后通过 Ctrl+F5 重新加载页面。
You need only for Development:
For Production (when using /manage.py collectstatic):
After Debug = False your static files will not loading. You can check it via entering to 127.0.0.1:8000/staticfiles/css/main.css (for example).
So, you need change to Debug = True and then reload the page via Ctrl+F5.