尝试将模型中的链接渲染为Img标记HTML

时间:2017-07-28 05:20:45

标签: python html django

Models.py:

class Product(models.Model):
    product_name = models.CharField(max_length=100)
    price = models.FloatField()
    weight = models.FloatField()
    image_link = models.URLField(max_length=500, default="http://polyureashop.studio.crasman.fi/pub/web/img/no-image.jpg")
    description = models.CharField(max_length=500)
    seller = models.ForeignKey('login_app.User')
    reviews = models.ManyToManyField('login_app.User', related_name="reviews")
    cart = models.ManyToManyField('login_app.User', related_name="carted")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = ProductManager()

Views.py:

def productdetails(request, product_id):
    try:
        request.session['user_id']
    except KeyError:
        return redirect("/")
    product = Product.objects.get(id = product_id)
    print product
    #users = User.objects.filter(joiners = plan_id).all()
    context = {
    'product': product,
    #"user":users,
    }
    return render(request, 'commerce/productdetails.html', context)

HTML:

<img src="{{product.image_link|urlize}}" alt="Image Not Found">

每当我在网页上运行此输出时: https://pisces.bbystatic.com/BestBuy_US/images/products/5190/5190001_sa.jpg;maxHeight=460;maxWidth=460“alt =”找不到图片“&gt;

1 个答案:

答案 0 :(得分:0)

像这样使用

<img src="{{ product.image_link }}" alt="Image Not Found">