无法从 /media/ 文件夹中获取图像,如果我尝试通过它的索引访问

这是我的设置.py


静止的


STATIC_URL = '/static/'

STATIC_DIR = [os.path.join(BASE_DIR, "static")]

媒体


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

URL 代码 - urls.py

from django.urls import path

from . import views


urlpatterns = [

path("", views.index, name="ShopHome"),

视图代码 - views.py

输入


from django.shortcuts import render

from . models import Product

from math import ceil

试图使滑块显示 4 个产品(一次)并遍历数据库中的产品列表


def index(request):

        products = Product.objects.all()

        print(products)

        n = len(products)

        nSlides = n//4 + ceil((n/4)-(n//4))

        params = {'no_of_slides':nSlides, 'range': range(1, nSlides), 'product': products}

        return render(request, 'shop/shop.html', params)

模板代码 - shop.html

{% load static %}

这工作正常。


    <div class="central-meta">

    <span class="create-post">Robotic Products <a href="#" title="">See All</a>


</span>

        <ul class="suggested-frnd-caro">

          <li>

            <img src="/media/{{product.0.image}}">

               <div class="sugtd-frnd-meta">

                   <a href="#" title="">{{product.0.product_name}}</a>

                    <span>2 mutual friend</span>

                    <ul class="add-remove-frnd">

                       <li class="add-tofrndlist"><a href="#" title="Add friend"><i class="fa fa-star"></i></a></li>

                       <li class="remove-frnd send-mesg"><a href="#" title="remove friend"><i class="fa fa-comment"></i></a></li>

                     </ul>

                </div>

                       </li>

但在这里我有问题无法遍历要显示的产品列表


子衿沉夜
浏览 130回答 1
1回答

守候你守候我

&nbsp; &nbsp; &nbsp; &nbsp;{% for i in product|slice:"1:"%}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="{{ i.img }}">&nbsp;<!--But in models.p at class&nbsp; product field img exist !!&nbsp; &nbsp; &nbsp;-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="sugtd-frnd-meta">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" title="">{{product.i.product_name}}</a> <!-- fetching from DB -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2 mutual friend</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="add-remove-frnd">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="add-tofrndlist"><a href="#" title="Add friend"><i class="fa fa-star"></i></a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="remove-frnd send-mesg"><a href="#" title="remove friend"><i class="fa fa-comment"></i></a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </div>&nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python