开发者

Satchmo, Want to change template depending on what product category is selected

I have two categories listed in my DB.

I would like to change the template (templates>product>category.html) depending on which category was selected. (It's because I want to change the colour scheme and header images per category)

How can I do this? Can I change the template that is being specified in the

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

which is in product.views?

Thanks


This is my category_view currently which returns a http500 error and an invalid syntax python django error.

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


If you look at the tutorials on the Django site and the other places in the documentation, you will find this handled in a way where it get's pretty easy to use different templates:

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜