我在将图像加载到Django Web应用程序时遇到问题。我已经使用了在线文档并尝试了在Stackoverflow上找到的人们的建议,但是我仍然没有运气来加载它。这是我所拥有的:
Settings.py:
STATIC_DIR = os.path.join(BASE_DIR,'static')
INSTALLED_APPS = [
...
'django.contrib.staticfiles'
]
STATIC_URL = '/static/'
STATIC_ROOT = [STATIC_DIR,]
Urls.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)
Index.html :(要点)
<!DOCTYPE html>
{% load static %}
<html>
<head>
</head>
<body>
<img src="{% static 'dir_app/tools.png' %}">
</body>
</html>
在此处存储图像:
答案 0 :(得分:1)
DEBUG
, django.conf.url.static
应该为true
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