Django staticfiles应用程序帮助 [英] Django staticfiles app help

查看:121
本文介绍了Django staticfiles应用程序帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django的 staticfiles应用程序有一些小问题

I've having a little issue with Django's staticfiles app.

我添加了

'django.contrib.staticfiles',

到我的INSTALLED_APPS,并添加了

to my INSTALLED_APPS and have added

STATIC_URL = '/static/'
STATIC_ROOT = '/Users/kevdotbadger/django/mylook/static/'

到我的 settings.py 文件。

我的所有静态文件位于Mac上的 STATIC_ROOT 文件夹中。

All my static files are located within the STATIC_ROOT folder on my Mac.

现在,在我的模板中,我使用

Now, within my template I use

{{ STATIC_URL }}

正确呈现到 / static /

然而

{{ STATIC_URL }}css/style.css

导致404错误。我正在使用'runserver'命令作为服务器。

result in a 404 error. I'm using the 'runserver' command as the server.

有没有我想要的东西?

推荐答案

我恳请你阅读这里的howto文档: http: //docs.djangoproject.com/en/dev/howto/static-files/

I implore you to read the howto docs here: http://docs.djangoproject.com/en/dev/howto/static-files/

简而言之:STATIC_ROOT仅在您调用收集管理命令。不需要将目录添加到STATICFILES_DIRS设置以提供静态文件!

In short: STATIC_ROOT is only used if you call the collectstatic manangement command. It's not needed to add the directory to the STATICFILES_DIRS setting to serve your static files!

在开发过程中(使用自动投放视图时)staticfiles将自动查找静态文件在各种位置(因为您称之为服务视图,并具有静态文件的路径)。对于这个搜索,它将使用所谓的查找者(在STATICFILES_FINDERS设置中定义)。

During development (when the automatic serving view is used) staticfiles will automatically look for static files in various locations (because you call its "serve" view with a path to a static file). For this search it'll use the so called "finders" (as defined in the STATICFILES_FINDERS setting).


  1. 默认查找器是AppDirectoriesFinder,它将在您的INSTALLED_APPS设置的每个应用程序的/ static /目录中查找。

  1. One of the default finders is the AppDirectoriesFinder, which will look in the "/static/" directory of each of the apps of yours INSTALLED_APPS setting.

另一个默认查找器是FileSystemFinder,它将在STATICFILES_DIRS设置中查找您指定的目录。

Another default finder is the FileSystemFinder, which will look in directories you specify in the STATICFILES_DIRS setting.

BTW,这两种搜索模式类似于模板加载如何工作。

BTW, both these search patterns are similar to how template loading works.

运行collectstatic命令时将使用相同的技术,除了现在将从各个位置收集文件(使用相同的查找器上面),将找到的文件放入STATIC_ROOT,准备部署。

The same technique will be used when running the collectstatic command, except that it now will collect the files from the various locations (using the same finders as above), putting the found files into STATIC_ROOT, ready for deployment.

这篇关于Django staticfiles应用程序帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆