【问题标题】:Satchmo, Want to change template depending on what product category is selectedSatchmo,想要根据选择的产品类别更改模板
【发布时间】:2011-02-26 09:52:43
【问题描述】:

我的数据库中列出了两个类别。

我想根据选择的类别更改模板(templates>product>category.html)。 (这是因为我想更改每个类别的配色方案和标题图像)

我该怎么做?我可以更改在

中指定的模板吗
def category_view(request, slug, parent_slugs='', template='product/category.html'):

product.views 中有哪些内容?

谢谢


这是我的 category_view 当前返回 http500 错误和无效语法 python django 错误。

def category_view(request, slug, parent_slugs='', template='product/category.html'):
    """Display the category, its child categories, and its products.

    Parameters:
     - slug: slug of category
     - parent_slugs: ignored
    """
    try:
        category =  Category.objects.get_by_site(slug=slug)
        products = list(category.active_products())
        sale = find_best_auto_discount(products)

    except Category.DoesNotExist:
        return bad_or_missing(request, _('The category you have requested does not exist.'))

    child_categories = category.get_all_children()

    ctx = {
        'category': category,
        'child_categories': child_categories,
        'sale' : sale,
        'products' : products,
    }

    if slug == 'healing-products'
        template = 'product/i.html'
    if slug == 'beauty-products'
        template ='product/category_beauty.html'

    index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products)
    return render_to_response(template, context_instance=RequestContext(request, ctx))

【问题讨论】:

    标签: django satchmo


    【解决方案1】:

    如果您查看 Django 站点上的教程和文档中的其他位置,您会发现处理方式很容易使用不同的模板:

    from django.shortcuts import render_to_response
    from django.template import RequestContext
    
    def category_view(request, slug, parent_slugs=''):
        if category=='category1':
            return render_to_response('template1',RequestContext(request))
        if category=='category2':
            return render_to_response('template2',RequestContext(request))  
    

    将模板作为函数参数传递只是能够将不同模板传递给视图的 satchmos 方式。但是您可以随时覆盖它。在此处仔细查看文档:http://docs.djangoproject.com/en/1.2/topics/http/views/

    【讨论】:

    • 请看我上面的代码。我尝试了类似于您建议的方法,但对我不起作用。
    • 知道原因会很有帮助。你能发布你的错误信息吗?
    • 如果你看看我的 if 语句的结尾,你会发现我遗漏了:
    • 哦。您应该考虑使用 IDE。 :))
    • 语法错误是由于if slug == 'healing-products'之后的代码行尾缺少冒号引起的。您可以通过编译:python -m compileall $(pwd) 轻松验证当前目录中整个树中文件的语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多