【问题标题】:Django - static files gets 404 errorDjango - 静态文件得到 404 错误
【发布时间】:2016-04-08 00:54:00
【问题描述】:

我知道有很多关于这个话题的话题,但在我的情况下它对我没有帮助。我真的很困惑。我的应用看不到我的静态文件。

我的应用程序的结构:

myapp-
  setting-
       settings.py
       url.py
  app
       templates
       static

Settings.py

PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'

STATICFILES_FINDERS = [
       "django.contrib.staticfiles.finders.FileSystemFinder",
       "django.contrib.staticfiles.finders.AppDirectoriesFinder"
]

urls.py

urlpatterns += patterns('', (
    r'^static/(?P<path>.*)$',
    'django.views.static.serve',
    {'document_root': settings.STATIC_ROOT}
))

模板

{% load staticfiles %}
<link href="{% static 'bower_components/metisMenu/dist/metisMenu.min.css' %}" rel="stylesheet" media="screen">

我做错了什么?谢谢!

【问题讨论】:

    标签: python django django-templates django-staticfiles


    【解决方案1】:

    添加这个

    STATICFILES_DIRS = (os.path.join(BASE_DIR, 'app/static'),)
    

    我现在不知道你的css的完整路径是什么,但你不需要完整的,如果它在static目录中,只需
    {% static 'your_file.css' %}

    【讨论】:

    • django.contrib.staticfiles.finders.AppDirectoriesFinder 不是在所有应用中搜索吗?
    • 很好,所以无论如何你都需要它,为你高兴:)
    • 我遇到了同样的问题,对我有用的是,在末尾添加一个正斜杠,即 os.path.join(BASE_DIR,'static/')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2021-11-25
    • 1970-01-01
    • 2021-02-28
    • 2019-01-23
    • 2018-07-21
    • 2020-05-07
    相关资源
    最近更新 更多