在Django中下载产品网址时出现问题

时间:2019-06-13 10:07:43

标签: python html django django-models

有两个型号productorder,如果产品是可下载的并且已订购,则用户可以在用户个人资料中下载该产品,请困在这里任何人都可以提供帮助。

产品型号

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

订单模型

class Order(models.Model):
    ref_code = models.CharField(max_length=15)
    owner = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True)
    is_ordered = models.BooleanField(default=False)
    items = models.ManyToManyField(OrderItem)
    date_ordered = models.DateTimeField(auto_now=True)

    def get_cart_items(self):
        return self.items.all()

    def get_cart_total(self):
        return sum([item.product.price for item in self.items.all()])

    def __str__(self):
        return '{0} - {1}'.format(self.owner, self.ref_code)

如果它包含要下载的产品的订单应下载到用户个人资料,请尝试从产品模式传递下载部分。

用户个人资料views.py

def check_product(user,product):
    if product in user.profile.ebooks.all():
        return True
    else:
        return False



def my_profile(request):
    my_user_profile = Profile.objects.filter(user=request.user).first()
    my_orders = Order.objects.filter(is_ordered=True, owner=my_user_profile)
    product = Product.objects.filter(download=True)
    if product:
        downloadable = check_product(request.user,product)

    context = {
        'my_orders': my_orders,
        'product':product,
        #'downloadable':downloadable
    }

    return render(request, "profile.html", context)

个人资料模板

{% 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 and downloadable %}
          <td><a href='{{ product.download|filename }}'>Download</a></td>
          {% endif %}

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

0 个答案:

没有答案