Django 静态文件应用程序帮助 [英] Django staticfiles app help

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

问题描述

我在使用 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 仅在您调用 collectstatic 管理命令时使用.无需将目录添加到 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, 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 静态文件应用程序帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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