【问题标题】:Custom product template in satchmosatchmo 中的自定义产品模板
【发布时间】:2010-01-29 22:35:18
【问题描述】:

我正在 satchmo 中实现一个商店。我通过使用产品模型的模型继承创建了一个自定义产品 MyProduct(如 http://thisismedium.com/tech/satchmo-diaries-part-one/ 所示)。

现在我想为 MyProduct 提供一个自定义产品详细信息模板,并且只有 MyProduct。我尝试在

中创建一个模板
/project/templates/product/product.html

但这会覆盖商店中所有产品的模板,而不仅仅是 MyProduct。我也试过了:

/project/templates/product/detail_myproduct.html
/project/templates/product/myproduct.html

但这些似乎都不起作用。

【问题讨论】:

    标签: django templates satchmo


    【解决方案1】:

    您的第一个猜测是正确的:templates/product/product.html。

    如果 MyProduct 是这样写的:

    class MyProduct(Product):
        # ...
        steele_level = model.IntegerField()
    
        objects = ProductManager()  # using this object manager is key!
    

    并在管理员处注册:

    admin.site.regsiter(MyProduct)
    

    然后您应该能够在管理员中创建一个新的 MyProduct,然后在 product/product.html 中关闭产品的 myproduct 属性:

    {% if product.myproduct %}
        This is a MyProduct with Steele Level: {{ product.myproduct.steele_level }}!
    {% endif %}
    

    或者,如果您更喜欢在 ./manage.py shell 中乱搞:

    from project.models import MyProduct
    from satchmo_store.shop.models import Product
    
    for p in Product.objects.all():
        print p 
        if hasattr(p, 'myproduct'):
            print "  >>> That was a MyProduct with steele_level %s" % p.myproduct.steele_level
    

    【讨论】:

    • 完美,谢谢!对我来说唯一的问题是 django 继续并小写了 MyProduct。所以我需要做 product.myproduct (就像你写的那样),而不是复制我的型号名称 product.MyProduct。
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2018-08-19
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多