问题描述
我正在尝试将图片加载到Django 3.1模板中。我遵循了https://docs.djangoproject.com/en/3.0/howto/static-files/上的文档,并在其他地方寻找答案,但无法解决问题。
以下是我认为相关的所有代码:
来自settings.py
INSTALLED_APPS = [
'memberships.apps.MembershipsConfig','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',]
STATICFILES_Dirs = [
os.path.join(BASE_DIR,"static"),'/var/www/static/',]
来自url.py
urlpatterns = [
path('',views.index,name='index'),path('memberships/',include('memberships.urls')),path('admin/',admin.site.urls),] + static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
并来自模板文件:
{% load static %}
...
<img src="{% static 'static/dribblz/images/multicolored-soccer-ball-on-green-field-47730.png' %}" alt="Football on the pitch">
我的文件结构如下:
dribblz /
-> dribblz /
-> settings.py
-> url.py
-> views.py
-> static / dribblz / images / image.png
-> templates / home.html
我不知道我还能提供什么其他有用的信息。 预先感谢。
解决方法
static已经在/static
之前加上了生成的网址,
因此您无需再次添加它:{% static 'dribblz/images/multicolored-soccer-ball-on-green-field-47730.png' %}