2011-02-26 44 views
0

我在数据库中列出了两个类别。Satchmo,想要根据所选产品类别更改模板

我想要更改模板(模板>产品> category.html)取决于所选类别。 (这是因为我想更改每个类别的配色方案和标题图像)

我该怎么做?我是否可以更改

def category_view(request, slug, parent_slugs='', template='product/category.html'): 

中指定的模板,它位于product.views?

感谢


这是目前我category_view它返回HTTP500错误和无效的语法蟒蛇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)) 

回答

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/

+0

请看我上面的代码。我尝试了一些类似于你所建议的但不适合我的东西。 – darren 2011-02-26 11:49:43

+0

会有帮助知道为什么。你能发布你的错误消息吗? – marue 2011-02-26 15:03:21

+0

如果你看看我的if语句结尾,你会看到我忘了: – darren 2011-02-27 07:22:27

相关问题