无法访问Django中的静态文件-开发服务器

时间:2020-06-19 11:04:52

标签: python django django-staticfiles

我正在尝试在浏览器中访问静态文件,但无法访问它们。我在settings.py中的静态设置如下

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

我的urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', FrontPage.as_view()),
    # url('', include(router.urls))
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

我的文件夹结构

projectname
       projectname
             settings.py
             urls.py
       app1_name
       static
             bs4
                  css
                     cover.css
             admin
       templates

我的template.html

<link href="/static/bs4/css/cover.css" rel="stylesheet">

以上链接未加载静态文件。我在到达上述网址时收到404。请让我知道我要去哪里错了。

enter image description here

2 个答案:

答案 0 :(得分:1)

您所需要做的就是更新设置:

STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]

答案 1 :(得分:0)

使用:<link href="{% static 'bootstrap/css/cover.css' %}" rel="stylesheet">

确保在template.html顶部使用{% load static %},一切都准备就绪。