簡體   English   中英

Django:Static 圖像未加載

[英]Django: Static Image Not Loading

我在將圖像加載到我的 Django web 應用程序時遇到問題。 我已經使用了在線文檔並嘗試了我在 Stackoverflow 上找到的人們的建議,但我仍然沒有任何運氣來加載它。 這是我所擁有的:

設置.py:

STATIC_DIR =  os.path.join(BASE_DIR,'static')

INSTALLED_APPS = [
...
'django.contrib.staticfiles'
]

STATIC_URL = '/static/'
STATIC_ROOT = [STATIC_DIR,]

網址.py:

from django.urls import path
from dir_app import views
from django.conf import settings
from django.conf.urls.static import static

app_name = 'dir_app'

urlpatterns=[
   path('', views.tradesmen, name='tradesmen'),
   path('register', views.register,name='register'),
   path('user_login', views.user_login,name='user_login')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

索引.html:(要點)

 <!DOCTYPE html>
 {% load static %}   
  <html>
   <head>
   </head>
   <body>
     <img src="{% static 'dir_app/tools.png' %}">
   </body>
  </html>

這是存儲圖像的位置:

圖像的位置

DEBUG should be true if want to use django.conf.url.static https://github.com/django/django/blob/926148ef019abcac3a9988c78734d9336d69f24e/django/conf/urls/static.py#L23

STATIC_ROOT應該是一個字符串,而不是一個列表。 https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATIC_ROOT

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM