Python Django - 页面未找到错误 (404) - 静态错误

1 投票
1 回答
2395 浏览
提问于 2025-04-18 08:05

我不知怎么的,应用程序在django上运行起来了(我对python和django都很陌生),虽然页面可以加载默认的URL(127.0.0.1:8000),但是它却无法加载css文件。直接访问css文件时会出现以下错误。

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/static/css/bootstrap.min.css
'css\bootstrap.min.css' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

这是我的settings.py文件:

STATIC_URL = '/static/'

# Template location
TEMPLATE_DIRS = {
    os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),

}

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static-only")
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "media")
    STATICFLIES_DIRS = (
        os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static")

    )

这是我的urls.py文件:

from django.conf.urls import patterns, include, url

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



from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'signups.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),

)


if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)

    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

有人能帮我解决这个问题吗?

1 个回答

0

在你往项目里添加任何静态文件后,你需要运行一下这个命令:

python manage.py collectstatic

撰写回答