django - Django - 静态图像渲染
问题描述
我有一个模板应该呈现每个产品的图片。但结果 - 它的 URL 在路径中有额外的“静态”
像这样 - 127.0.0.1:8000/静态/静态/照片/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')
视图.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 %}
解决方案
改变这个
{% 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 %}
推荐阅读
- xslt - XSL 中的替换功能问题
- c# - 你可以在 Xamarin.Forms 中使用 Firebase.Auth 吗?
- python - 将行转置为python列
- android - Kotlin Android 扩展:java.lang.IllegalStateException:视图不能为空
- buffer - Vulkan,多个对象的相同统一缓冲区?
- nativescript - Nativescript - Android TabView 后退按钮自定义导航
- javascript - React-Redux - 在组件中的另一个请求中使用来自 componentDidMount 中的获取请求的响应
- clojure - 拆分字符串时clojure java.lang.NullPointerException
- node.js - Nodejs识别客户端ssl
- ios - 如何将可映射对象保存到 nsuserdefault?