如果特定产品可下载,我如何通过链接下载产品
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
{% 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 %}
我是否在这里缺少任何内容,尽管下载功能还不错,但我无法使下载链接正常工作。 打算的是,如果购买了该产品,请转至我的用户个人资料,如果该产品是可下架产品,则用户可以在个人资料中下载该产品。 帮帮忙。
答案 0 :(得分:0)
您应该使用{{ product.download.url }}