简体   繁体   English

Django 1.7-提供静态文件

[英]Django 1.7 - Serving static files

I'm following the official documentation in order to serve static files but I'm recieving error 404 in the development console. 我正在遵循官方文档以提供静态文件,但是在开发控制台中收到错误404。 I'm using 'django.contrib.staticfiles', so static files should be automatically served. 我正在使用“ django.contrib.staticfiles”,因此应该自动提供静态文件。 This is my setup: 这是我的设置:

Settings: 设置:

STATIC_ROOT = ''
STATIC_URL = '/static/'

Template header: 模板标题:

{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/app.css" %}">

Directory tree: 目录树:

django_project
    \apps
    \static
        \css
            app.css
    \templates
        index.html

I can see in firefox console that the path to my file is correct: 我可以在firefox控制台中看到我文件的路径是正确的:

在此处输入图片说明

So the problem must be that Django is not serving static files. 因此,问题一定是Django不提供静态文件。 I can't find what I'm missing. 我找不到我想要的东西。 Any advice is more than welcome. 任何建议都值得欢迎。

SOLUTION: I was missing this line in my settings.py 解决方案:我在settings.py中缺少此行

STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static'),)

It looks it's mandatory, same as TEMPLATE_DIRS. 看起来它是强制性的,与TEMPLATE_DIRS相同。

When you run collectstatic it puts all your static content in the path specified by STATIC_ROOT. 当您运行collectstatic时,它将所有静态内容放入STATIC_ROOT指定的路径中。 Check the deploy to production docs 检查部署到生产文档

If you are using the django server try checking the path that is being generated by {% static %}...you may have some trailing slash or something missing. 如果您使用的是django服务器,请尝试检查{%static%}生成的路径...您可能会有一些斜杠或缺少某些内容。

Check that you have are following all the requirements . 检查您是否遵守所有要求 You need to have django.contrib.staticfiles in your installed apps and something like this in your main urls file: 您需要在已安装的应用程序中拥有django.contrib.static文件,并且在主url文件中具有以下内容:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

That should work :) 那应该工作:)

settings.py settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'project', "static"),
)

example of context_processors from settings.py: settings.py中的context_processors示例:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    'django.core.context_processors.request',
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
)

example of installed apps in settings.py: settings.py中已安装应用程序的示例:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

urls.py: urls.py:

from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
    urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

For anyone running django-cms and experiencing 404 errors (particularly, all of your static files are having "en-us" prepended to the URL), I found the following steps to help. 对于运行django-cms并遇到404错误(特别是您的所有静态文件的URL都以“ en-us”开头)的任何人,我发现可以采取以下步骤。

First, turn off internationalization of pattern matching in your urls.py file, as described here : 首先,请在您的urls.py文件中关闭模式匹配的国际化功能, 如下所示

urlpatterns = i18n_patterns('',
      url(r'^admin/', include(admin.site.urls)),
      url(r'^', include('cms.urls')),
)

should instead be: 相反,应为:

from django.conf.urls import patterns

urlpatterns = patterns('',
  url(r'^admin/', include(admin.site.urls)),
  url(r'^', include('cms.urls')),
)

The import is important, because the configuration of django-cms removes the patterns import from django.conf.urls . 导入很重要,因为django-cms的配置会删除从django.conf.urls导入的patterns

This solved the redirect, but it still wasn't finding my static files. 这解决了重定向问题,但仍然找不到我的静态文件。 I needed to manually add the static url to the url patterns, like this: 我需要手动将静态网址添加到网址格式中,如下所示:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

After that, static files worked as expected. 在那之后,静态文件按预期工作。

I'm sure that this is probably related to me messing up my configuration as a complete novice to Django. 我确定这可能与我把我的配置作为Django的新手搞砸有关。 But since others might have the same problems, I'm putting it out there as a possible, if less than ideal, solution. 但是由于其他人可能也有同样的问题,因此我将其作为一种可能的解决方案,即使不理想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM