我有一个模板,可以显示每个产品的图片。 但是结果是-它的URL在路径中有额外的“静态”
像这样-127.0.0.1:8000/ 静态 /static/photos/product1.png。
应该仅为127.0.0.1:8000/static/photos/product1.png
有什么办法可以正确地做到这一点?
model.py 将其保存到“静态/照片”
class Product(models.Model):
title = models.CharField(max_length=20)
photo = models.ImageField(upload_to='static/photos', default='http://placehold.it/700x400')
views.py
from django.shortcuts import render
# Create your views here.
from .models import Category, Product
def product_list(request):
queryset = Product.objects.all()
context = {"object_list": queryset}
return render(request, "product_list.html", context)
template.html 正在关注
{% load static %}
{% for instance in object_list %}
<p><img src="{% static instance.photo %}" /></p>
{% endfor %}
答案 0 :(得分:0)
更改此
{% for instance in object_list %}
<p><img src="{% static instance.photo %}" /></p>
{% endfor %}
收件人
{% for instance in object_list %}
<p><img src="{% static instance.photo.url %}" /></p>
{% endfor %}