0

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

我想根据选择的类别更改模板(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))
4

1 回答 1

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))  

Passing the template as a function parameter is just satchmos way to be able to pass different templates to the view. But you can override that any time. Have a closer look at the docs here: http://docs.djangoproject.com/en/1.2/topics/http/views/

于 2011-02-26T10:03:30.353 回答