我无法正确指向静态图像文件
I could not point correctly the static image files
index.html
<div class="carousel-inner" role="listbox">
{% for pizza in pizza_list %}
{% if forloop.counter0 == 0 %}
<div class="carousel-item active">
<img width="250" height="850" class="d-block w-100 h-50" class="h-50 d-inline-block" src="{% static 'Pizza/{{ pizza }}' %}" alt="...">
</div>
{% else %}
<div class="carousel-item">
<img width="250" height="850" class="d-block w-100 h-50" class="h-50 d-inline-block" src="{% static 'Pizza/{{ pizza }}' %}" alt="...">
</div>
{% endif %}
{% endfor %}
</div>
views.py
def index(request):
pizza_list = []
print(os.getcwd())
for item in os.listdir('static/Pizza'):
pizza_list.append(item)
## print(pizza_list)
context = {'pizza_list':pizza_list}
return render(request,'index.html',context)
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
urls.py 在 Main_Project
文件夹下
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('pizza.urls')),
]
urlpatterns += static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
我的项目目录
Main_Project (top)
--Main_Project (top)
--settings
--urls
--pizza
--static
--Pizza
--(images)
--templates
--migrations
--views.py
--models.py
--urls.py
在这个Django项目中,我对静态图像和文件应该放在哪里感到困惑,因为我无法加载我保存在静态文件夹目录中的index.html
中的图像。另外,我不确定我的设置是否正确以便在模板上显示图像,但是当我尝试打印 pizza_list
时它可以显示图像文件名。请问最好的文件夹结构是什么才能让模板识别静态文件夹中的图像文件来显示?
你{% load static %}了吗?
我假设 {{ pizza }} 变量是一个图像。如果是这样,那么使用这个:
<img src="{% static 'Pizza/{{ pizza.url }}' %}" />
index.html
<div class="carousel-inner" role="listbox">
{% for pizza in pizza_list %}
{% if forloop.counter0 == 0 %}
<div class="carousel-item active">
<img width="250" height="850" class="d-block w-100 h-50" class="h-50 d-inline-block" src="{% static 'Pizza/{{ pizza }}' %}" alt="...">
</div>
{% else %}
<div class="carousel-item">
<img width="250" height="850" class="d-block w-100 h-50" class="h-50 d-inline-block" src="{% static 'Pizza/{{ pizza }}' %}" alt="...">
</div>
{% endif %}
{% endfor %}
</div>
views.py
def index(request):
pizza_list = []
print(os.getcwd())
for item in os.listdir('static/Pizza'):
pizza_list.append(item)
## print(pizza_list)
context = {'pizza_list':pizza_list}
return render(request,'index.html',context)
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
urls.py 在 Main_Project
文件夹下
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('pizza.urls')),
]
urlpatterns += static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
我的项目目录
Main_Project (top)
--Main_Project (top)
--settings
--urls
--pizza
--static
--Pizza
--(images)
--templates
--migrations
--views.py
--models.py
--urls.py
在这个Django项目中,我对静态图像和文件应该放在哪里感到困惑,因为我无法加载我保存在静态文件夹目录中的index.html
中的图像。另外,我不确定我的设置是否正确以便在模板上显示图像,但是当我尝试打印 pizza_list
时它可以显示图像文件名。请问最好的文件夹结构是什么才能让模板识别静态文件夹中的图像文件来显示?
你{% load static %}了吗?
我假设 {{ pizza }} 变量是一个图像。如果是这样,那么使用这个:
<img src="{% static 'Pizza/{{ pizza.url }}' %}" />