Django 1.7 - Serving static files

こ雲淡風輕ζ 提交于 2019-12-02 19:11:34

问题


I'm following the official documentation in order to serve static files but I'm recieving error 404 in the development console. I'm using 'django.contrib.staticfiles', so static files should be automatically served. 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:

So the problem must be that Django is not serving static files. I can't find what I'm missing. Any advice is more than welcome.


回答1:


SOLUTION: I was missing this line in my settings.py

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

It looks it's mandatory, same as TEMPLATE_DIRS.




回答2:


When you run collectstatic it puts all your static content in the path specified by 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.

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:

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)



回答3:


That should work :)

settings.py

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

example of context_processors from settings.py:

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:

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

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)



回答4:


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.

First, turn off internationalization of pattern matching in your urls.py file, as described here:

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.

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. But since others might have the same problems, I'm putting it out there as a possible, if less than ideal, solution.



来源:https://stackoverflow.com/questions/26580700/django-1-7-serving-static-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!