Django静态文件不在根目录中时加载

时间:2020-01-21 16:20:58

标签: python django

我将静态文件存储在项目根目录中,但是无法将其加载到浏览器中。

  • 配置
  1. settings.py
STATIC_DIR = os.path.join(BASE_DIR, 'static')  

STATIC_URL = '/static/'
STATICFILES_DIR = [
    STATIC_DIR,
]  
  1. 模板文件
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>Index</title>
    </head>
    <body>
        <h1>{{temp}}</h1>
        <h2><a href="#">Click here</a> to veiw user details</h2>
        <img src="{% static "user/images/smile.png"%}" alt="oh oh didn't show"/>
    </body>
</html>  

1 个答案:

答案 0 :(得分:0)

在文件urls.py中添加静态网址。

urlpatterns = [
    path('admin/', admin.site.urls),
    ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)