【问题标题】:how to show external media in template in django如何在 django 的模板中显示外部媒体
【发布时间】:2019-04-15 20:58:40
【问题描述】:

您好,我目前是 Django 新手,我正在尝试填充产品页面。

我在使用 img 来显示图像时遇到问题(它使用在线图像 url 而不是文件),例如 img src="media3.scdn.vn/img2/2018/6_2/ZIBLXA_simg_b5529c_250x250_maxb.jpg"

我的数据库中已经存在带有文本media3.scdn.vn/img2/2018/6_2/ZIBLXA_simg_b5529c_250x250_maxb.jpg的网址

但是当我尝试在模板中渲染它时,图像没有显示 我尝试使用但它仍然无法正常工作 任何帮助将不胜感激!

我的模板

 {% for discount in discounts|slice:":8" %}
                <div class="col-md-3 product-men women_two">
                    <div class="product-googles-info googles">
                        <div class="men-pro-item">
                            <div class="men-thumb-item">
                                <img src="{{STATIC_URL}}{{discount.product_image}}"  alt=""/>
                                <div class="men-cart-pro">
                                    <div class="inner-men-cart-pro">
                                        <a href="single .html" class="link-product-add-cart">Quick View</a>
                                    </div>
                                </div>
                                <span class="product-new-top">New</span>
                            </div>
                            <div class="item-info-product">
                                <div class="info-product-price">
                                    <div class="grid_meta">
                                        <div class="product_price">
                                            <h4>
                                                <a href="single.html">{{discount.product_name}}</a>
                                            </h4>
                                            <div class="grid-price mt-2">
                                                <span class="money ">{{discount.product_old_price}}</span>
                                            </div>
                                        </div>
                                        <div>
                                            <h3>{{discount.product_sit}}</h3>
                                        </div>
                                        <div><h2 style="color:red">Only {{discount.product_price}}!</h2></div>
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}

编辑

似乎 src 试图获取的图像是“http://127.0.0.1:8000/media3.scdn.vn/img2/2018/6_2/ZIBLXA_simg_b5529c_250x250_maxb.jpg”,我不希望链接中出现“http://127.0.0.1:8000/”。使用 {{discount.product_image}} 标签时有没有办法消除它?

主项目 urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('frontend.urls')),
]

前端应用 urls.py:

from  django.urls import path
from .import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'frontend'
urlpatterns = [
    #index
    path('',views.index, name='index')
]

【问题讨论】:

    标签: html django image


    【解决方案1】:

    尝试像这样使用.url

    <img src="{{ discount.product_image.url }}"  alt=""/>
    

    还将MEDIASTATIC 网址添加到您的项目网址模式中。

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        # ... the rest of your URL pattern goes here ...
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
    + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    编辑:

    通过聊天中的讨论,我了解到我首先误解了您的问题。图像是外部的,而不是在您的数据库中,您只是将 URL 存储到图像。而且您使用的是TextField 而不是ImageField。我以前假设ImageField。因此,您所要做的就是替换实际 URL 前面的 {{STATIC_URL}} 并将 http:// 放在那里。这背后的原因是,由于src 缺少http://,因此假定图像位于同一主机域中。所以它转到http://127.0.0.1/your_url

    <img src="http://{{ discount.product_image }}"  alt=""/>
    

    【讨论】:

    • 还是不行。它回来了 "[13/Nov/2018 13:53:49] "GET /media3.scdn.vn/img2/2018/6_8/3eJ0YO_simg_b5529c_250x250_maxb.jpg HTTP/1.1" 404 2290" 它似乎创建了一个 "/"在使链接错误的网址之前的网址
    • 另外,请确保您已正确设置MEDIA_URLMEDIA_ROOT STATIC_URLSTATIC_ROOT
    • 现在网站(主页)的其他 url 将无法使用 File "C:\Users\admin\Miniconda3\lib\site-packages\django-2.1-py3.6.egg\ django\conf\urls\static.py",第 21 行,在 static raise ImproperlyConfigured("Empty static prefix not allowed") django.core.exceptions.ImproperlyConfigured: Empty static prefix not allowed
    • 你没有正确设置STATIC_URLSTATIC_ROOT
    • 我的网站有不同的应用程序,所以我应该将它设置在主 url 还是每个应用程序的其他 url 中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2012-06-02
    • 2018-10-15
    • 2021-08-21
    • 1970-01-01
    • 2017-12-17
    • 2016-09-14
    相关资源
    最近更新 更多