Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中调用DetailView/ListView中的自定义方法?_Python_Html_Django - Fatal编程技术网

Python 如何在django中调用DetailView/ListView中的自定义方法?

Python 如何在django中调用DetailView/ListView中的自定义方法?,python,html,django,Python,Html,Django,我正在创建一个电子商务网站,并考虑添加一个自定义方法来检查登录用户是否购买了产品,然后我在ProductDetail视图中提出了“check_Buyed”方法,其中包含产品模型和ProductPurchase模型 现在我有以下问题需要您的帮助: 我应该重写哪个方法使其自动调用?我想到了重写get\u context\u data\u方法。如果check_done返回true,那么我将创建一个上下文['bund']='some str',并在html上呈现它。然而,我认为一定有更好的方法 我应该如

我正在创建一个电子商务网站,并考虑添加一个自定义方法来检查登录用户是否购买了产品,然后我在ProductDetail视图中提出了“check_Buyed”方法,其中包含产品模型和ProductPurchase模型

现在我有以下问题需要您的帮助:

  • 我应该重写哪个方法使其自动调用?我想到了重写get\u context\u data\u方法。如果check_done返回true,那么我将创建一个上下文['bund']='some str',并在html上呈现它。然而,我认为一定有更好的方法
  • 我应该如何修改ProductList视图使其也能工作
  • 最后,你们建议在html文件中显示什么
  • 我只是想知道你们会怎么解决这个问题。甚至在Prodcut模型中添加“购买人”这样的属性都是好还是坏?真的需要你的帮助!!非常感谢你

    视图.py

    型号.py


    1.我把它放在
    get\u context\u data
    方法中。2.如果在2个上下文中需要相同的函数,则在类外部编写一个helper函数,并从两个类中调用该函数。3.可能是只显示复选框?
     class ProductList(generic.ListView):
        model = Product
        def get_context_data(self, *args, **kwargs):
            context = super(ProductList,self).get_context_data(*args, **kwargs)
            cart_obj, new_obj = Cart.objects.new_or_get(self.request)
            context['cart'] = cart_obj
            return context
    
    
    class ProductDetail(ObjectViewedMixin,generic.DetailView):
        model = Product
        def get_context_data(self, *args, **kwargs):
            context = super(ProductDetail,self).get_context_data(*args, **kwargs)
            cart_obj, new_obj = Cart.objects.new_or_get(self.request)
            context['cart'] = cart_obj
    
            return context
    
        def check_bought(self):
            product_bought = False
            if self.request.user.is_authenticated:
                user_billing_profile = self.request.user.billing_profile
                if self.object.purchased.filter(billing_profile=user_billing_profile).count():
                    product_bought = True
            print(f'bought?:{product_bought}')
            return product_bought
    
        def get_ip_address(self):
            ip = object_viewed_signal.send(instance.__class__,instance=instance,request=request)
            return ip
    
    class ProductPurchase(models.Model):
        billing_profile     = models.ForeignKey(BillingProfile, on_delete=models.CASCADE) 
        order_id            = models.CharField(max_length=120)
        product             = models.ForeignKey(Product,on_delete=models.CASCADE,related_name='purchased')
        refunded            = models.BooleanField(default=False)
        timestamp           = models.DateTimeField(auto_now_add=True)
        updated             = models.DateTimeField(auto_now=True)
    
    
    class Product(models.Model):
        title           = models.CharField(max_length=120)
        slug            = models.SlugField(blank=True, unique=True)
        description     = models.TextField(blank=True)
        price           = models.DecimalField(decimal_places=2,max_digits=20)
        image           = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
        featured        = models.BooleanField(default=False)
        active          = models.BooleanField(default=True)
        timestamp       = models.DateTimeField(auto_now_add=True)
        is_digital      = models.BooleanField(default=False) # User Libary
    
        objects = ProductManager()