Python Django 静态文件应用程序帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4565935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:21:27  来源:igfitidea点击:

Django staticfiles app help

pythondjangostatichttp-status-code-404

提问by dotty

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

我在使用 Django 的staticfiles 应用程序时遇到了一个小问题。

I have added

我已经添加了

'django.contrib.staticfiles',

to my INSTALLED_APPS and have added

到我的 INSTALLED_APPS 并添加了

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

to my settings.pyfile.

到我的settings.py文件。

All my static files are located within the STATIC_ROOTfolder on my Mac.

我所有的静态文件都位于STATIC_ROOTMac 上的文件夹中。

Now, within my template I use

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

{{ STATIC_URL }}

which correctly renders to /static/.

正确呈现为/static/.

However

然而

{{ STATIC_URL }}css/style.css

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

导致 404 错误。我使用“runserver”命令作为服务器。

Is there something I'm missing?

有什么我想念的吗?

回答by C. Alan Zoppa

First of all, make sure that you have static serveenabled in your urls.py FOR DEVELOPMENT ONLY.

首先,确保您在 urls.py 中启用了静态服务,仅供开发使用。

Also, you may have an extra slash in there. If your STATIC_URL == '/static/', then

此外,您可能在那里有一个额外的斜线。如果您的 STATIC_URL == '/static/',则

{{ STATIC_URL }}/css/style.css should be {{ STATIC_URL }}css/style.css.

{{ STATIC_URL }}/css/style.css 应该是 {{ STATIC_URL }}css/style.css。

回答by jezdez

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

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

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!

简而言之:STATIC_ROOT 仅在您调用 collectstatic 管理命令时使用。不需要将目录添加到 STATICFILES_DIRS 设置来为您的静态文件提供服务!

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).

在开发过程中(当使用自动服务视图时)静态文件会自动在不同位置寻找静态文件(因为你用静态文件的路径调用它的“服务”视图)。对于此搜索,它将使用所谓的“查找程序​​”(在 STATICFILES_FINDERS 设置中定义)。

  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.

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

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

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

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

顺便说一句,这两种搜索模式都类似于模板加载的工作方式。

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.

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

回答by Rost

If you want just a solution - scroll down to the "Solution".

如果您只想要一个解决方案 - 向下滚动到“解决方案”。

Overview

概述

I was discovering the same problem yesterday. Here is what I found:

我昨天发现了同样的问题。这是我发现的:

All you need is appropriate static finder, that will find your STATIC_ROOT and all its contents, but there is no such finder. Here are default finders:

您所需要的只是适当的静态查找器,它将找到您的 STATIC_ROOT 及其所有内容,但没有这样的查找器。以下是默认查找器:

  • django.contrib.staticfiles.finders.AppDirectoriesFinder- search installed django applications dirs for 'static' folder, but most of them use obsolete 'media' folders for now.

  • django.contrib.staticfiles.finders.FileSystemFinder- use all dirs mentioned in the STATICFILES_DIRS, but you can't add STATIC_ROOT into it.

  • django.contrib.staticfiles.finders.DefaultStorageFinder- search static in your DEFAULT_FILE_STORAGE which is django.core.files.storage.FileSystemStorageby default and it points to your MEDIA_ROOT

  • django.contrib.staticfiles.finders.AppDirectoriesFinder- 搜索已安装的 django 应用程序目录中的“静态”文件夹,但其中大多数现在使用过时的“媒体”文件夹。

  • django.contrib.staticfiles.finders.FileSystemFinder- 使用 STATICFILES_DIRS 中提到的所有目录,但您不能将 STATIC_ROOT 添加到其中。

  • django.contrib.staticfiles.finders.DefaultStorageFinder- 在您的 DEFAULT_FILE_STORAGE 中搜索静态,默认情况下是django.core.files.storage.FileSystemStorage,它指向您的 MEDIA_ROOT

Solution

解决方案

That's all, no additional choices. There are no choices to use STATIC_ROOT for now (in Django 1.3).
So I've just wrote my own custom finder for these needs:

就是这样,没有额外的选择。目前没有使用 STATIC_ROOT 的选择(在 Django 1.3 中)。
所以我刚刚为这些需求编写了自己的自定义查找器:

  • My custom static finder file: finders.py:

    from django.core.files.storage import FileSystemStorage
    from django.contrib.staticfiles.finders import BaseStorageFinder
    from django.conf import settings
    
    class StaticFinder(BaseStorageFinder):
        storage = FileSystemStorage(settings.STATIC_ROOT, settings.STATIC_URL)
    
  • settings.py:

    STATICFILES_FINDERS = (
        'finders.StaticFinder',
    )
    
  • If you want to use it with another finders - I suggest to put them after it inside STATICFILES_FINDERS
  • 我的自定义静态查找器文件:finders.py

    from django.core.files.storage import FileSystemStorage
    from django.contrib.staticfiles.finders import BaseStorageFinder
    from django.conf import settings
    
    class StaticFinder(BaseStorageFinder):
        storage = FileSystemStorage(settings.STATIC_ROOT, settings.STATIC_URL)
    
  • 设置.py

    STATICFILES_FINDERS = (
        'finders.StaticFinder',
    )
    
  • 如果您想将它与其他查找器一起使用 - 我建议将它们放在 STATICFILES_FINDERS 之后

And remember: this solution should be used only in development needs and never on production!

请记住:此解决方案应仅用于开发需求,切勿用于生产!

回答by maruusos82

STATIC_ROOT = '/home/ws/be/bla/'
STATICFILES_DIRS = ('/home/ws/be/static/',)
STATIC_URL = '/static/'

This works for me. STATIC_ROOT must differ from STATICFILES_DIRS (Django version 1.4 pre-alpha SVN-16436)

这对我有用。STATIC_ROOT 必须不同于 STATICFILES_DIRS(Django version 1.4 pre-alpha SVN-16436)

回答by Markus Lenger

I found a quick and easy workaround to serve project global static files during development:

我找到了一种快速简便的解决方法,可以在开发过程中为项目全局静态文件提供服务:

  • Start a new app that contains your projects static files (e.g. "manage.py startapp static_content")
  • Create a folder named 'static' in that app and put your static files there.
  • Add your new app to the list of installed apps
  • 启动一个包含项目静态文件的新应用程序(例如“manage.py startapp static_content”)
  • 在该应用程序中创建一个名为“static”的文件夹,并将您的静态文件放在那里。
  • 将您的新应用添加到已安装应用列表中