如何在Django模板中传递产品下载链接 [英] How to pass a product download link inside the django template

查看:63
本文介绍了如何在Django模板中传递产品下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果特定产品可下载,我如何通过链接下载产品

how do i pass the link to download a product if the particular product is downloadable

protected_loc = settings.PROTECTED_UPLOADS

def download_loc(instance,filename):
    return "%s/%s" %(instance.slug, filename)
    # if instance.user.username:
    #     return "%s/download/%s" %(instance.user.username,filename)
    # else:
    #     return "%s/download/%s" %("default",filename)


class Product(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
    name = models.CharField(max_length=120)
    description = models.CharField(max_length=500,null=True,blank=True)
    download = models.FileField(upload_to=download_loc,storage=FileSystemStorage(location=protected_loc),null=True,blank=True)
    price = models.DecimalField(max_digits=60,decimal_places=2)
    sale_price = models.DecimalField(max_digits=60,decimal_places=2,null=True,blank=True)
    slug= models.SlugField()

    def __str__(self):
        return self.name



模板



template

 {% for order in my_orders %}
        <tr>
          <td>{{ order.date_ordered }}</td>
          <td>{{ order.ref_code }}</td>
          <td>
            {% for item in order.items.all %}
                {{ item.product.name }}

            {% endfor %}
          </td>
          <td>${{ order.get_cart_total }}</td>
          {% if product.download %}
          <td><a href='{{ product.download|filename }}'>Download</a></td>
          {% endif %}

        </tr>
      {% empty %}
        <tr>
          <td colspan= 4> You have no orders.</td>
        </tr>
      {% endfor %}

我在这里丢失了一些东西我无法获得下载链接到尽管下载功能很好,但是可以工作。
的打算是,如果购买了该产品,请转到我的用户个人资料,如果该产品是可下架产品,则用户可以在个人资料中下载该产品。
帮忙。

Am i missing something here i Cant get the download link to work though the download function is fine . What am intending is that if the product is purchased-----> go to my userprofile where in the profile user can be able to download it if it is a downlodable product. help out.

推荐答案

您应使用 {{product.download.url}}

这篇关于如何在Django模板中传递产品下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆