How to pass a product download link inside the dja

2020-05-02 05:36发布

问题:

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

models.py

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.

回答1:

You should use {{ product.download.url }}