Django 1.7 - Serving static files

前端 未结 4 1428
执念已碎
执念已碎 2021-01-28 08:22

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 s

相关标签:
4条回答
  • 2021-01-28 08:32

    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)
    
    0 讨论(0)
  • 2021-01-28 08:35

    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)
    
    0 讨论(0)
  • 2021-01-28 08:42

    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.

    0 讨论(0)
  • 2021-01-28 08:51

    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.

    0 讨论(0)
提交回复
热议问题