Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行for循环获取图像时未显示图像_Python_Django - Fatal编程技术网

Python 运行for循环获取图像时未显示图像

Python 运行for循环获取图像时未显示图像,python,django,Python,Django,我在index.html文件中有一个for循环,用于从images文件夹中动态获取三个对象的图像 但它只获取第一个图像,服务器上会为其他两个“目标”对象显示缺少的图像图标。在index.html和views.py文件中是否有突出的错误 所有三个图像都是在硬编码时显示的,并且都存在于images文件夹中。自从我引入了{%static“images”作为baseUrl%}和for循环以来,它就不起作用了。 奇怪的是,它确实加载了第一个图像,但没有加载图像2和图像3 我还尝试: src=“static

我在
index.html
文件中有一个for循环,用于从images文件夹中动态获取三个对象的图像

但它只获取第一个图像,服务器上会为其他两个“目标”对象显示缺少的图像图标。在
index.html
views.py
文件中是否有突出的错误

所有三个图像都是在硬编码时显示的,并且都存在于images文件夹中。自从我引入了
{%static“images”作为baseUrl%}
for循环以来,它就不起作用了。
奇怪的是,它确实加载了第一个图像,但没有加载图像2和图像3

我还尝试:
src=“static/images/{{dest.img}}”
而不是
src=“{%static”images/{{{dest.img}}”%}
,这不起作用

下面是
for循环
代码,其中包含
index.html
中的顶级导入:

{% load static %}
{% static "images" as baseUrl %}


                {% for dest in dests %}

                <!-- Destination -->
                <div class="destination item">
                    <div class="destination_image">
                        <img src="{{baseUrl}}/{{dest.img}}" alt="">
                        <div class="spec_offer text-center"><a href="#">Special Offer</a></div>
                    </div>
                    <div class="destination_content">
                        <div class="destination_title"><a href="destinations.html">{{dest.name}}</a></div>
                        <div class="destination_subtitle"><p>{{dest.desc}}</p></div>
                        <div class="destination_price">From ${{dest.price}}</div>
                    </div>
                </div>

                {% endfor %}

如果您的图像被上传,您无法像这样获取它
dest.img
只需像
dest.img.url
那样操作,并且不需要将此
{%static“images”加载为baseUrl%}
因为这是在您从静态镜像图像时使用的,但是当您开始处理数据库并上载图像时,您必须像下面这样做

{% load static %}

                {% for dest in dests %}

                <!-- Destination -->
                <div class="destination item">
                    <div class="destination_image">
                        <img src="{{dest.img.url}}" alt="">
                        <div class="spec_offer text-center"><a href="#">Special Offer</a></div>
                    </div>
                    <div class="destination_content">
                        <div class="destination_title"><a href="destinations.html">{{dest.name}}</a></div>
                        <div class="destination_subtitle"><p>{{dest.desc}}</p></div>
                        <div class="destination_price">From ${{dest.price}}</div>
                    </div>
                </div>

                {% endfor %}
{%load static%}
{dest%中dest的%s}
{{dest.desc}}

从${dest.price} {%endfor%}

如果这对您没有帮助,请让我知道是否还有任何进一步的帮助

我得到了它,变量分配有问题。这就是为什么它没有为
{{dest.img}
返回任何字符串,因此无法链接到图像。您分配了多个
dest1.img
。请参阅固定解决方案:

def index(request):

    dest1 = Destination()
    dest1.name = 'New York'
    dest1.desc = 'City that never sleeps'
    dest1.img = 'destination_1.jpg'
    dest1.price = 500

    dest2 = Destination()
    dest2.name = 'Mumbai'
    dest2.desc = 'Great nightlife'
    dest2.img = 'destination_2.jpg' # wrong variable assignment
    dest2.price = 800

    dest3 = Destination()
    dest3.name = 'Paris'
    dest3.desc = 'The City Of Culinary Delights'
    dest3.img = 'destination_3.jpg' # wrong variable assignment
    dest3.price = 700

    dests = [dest1, dest2, dest3]
    #     'dest1': dest1,
    #     'dest2': dest2,
    #     'dest3': dest3,
    # }

    return render(request, 'index.html', {'dests': dests})

dest2和dest3中是否还有其他内容按预期呈现?是的,字幕、价格等。dest2和dest3都有。只是图像没有显示。好的。请检查开发工具中呈现的html并发布图像部分?然后我们可以调试img src,看看有什么错误。它说当我点击整个页面的错误。我现在将检查单个图像的image2。在图像行上。好的,那么src不正确。继续调查访问站点时在html中呈现的src。然后您将看到src字符串有什么问题。我刚才尝试过这个,但当我重新显示时,它根本没有显示任何图像用dest.img.url替换旧的静态代码。你的图像是来自上传的图像还是来自收集的静态文件图像来自收集的静态文件。但是我看到了你今后的发展方向,即何时上传图像而不是将图像设置为静态,并感谢您的帮助它们是马车吗?你是说我必须删除它们吗?@cheznead不,只需复制我的代码,并将这两行代码与你的代码进行比较即可
def index(request):

    dest1 = Destination()
    dest1.name = 'New York'
    dest1.desc = 'City that never sleeps'
    dest1.img = 'destination_1.jpg'
    dest1.price = 500

    dest2 = Destination()
    dest2.name = 'Mumbai'
    dest2.desc = 'Great nightlife'
    dest2.img = 'destination_2.jpg' # wrong variable assignment
    dest2.price = 800

    dest3 = Destination()
    dest3.name = 'Paris'
    dest3.desc = 'The City Of Culinary Delights'
    dest3.img = 'destination_3.jpg' # wrong variable assignment
    dest3.price = 700

    dests = [dest1, dest2, dest3]
    #     'dest1': dest1,
    #     'dest2': dest2,
    #     'dest3': dest3,
    # }

    return render(request, 'index.html', {'dests': dests})