0

DBに2つのカテゴリがリストされています。

選択したカテゴリに応じて、テンプレート(templates> product> category.html)を変更したいと思います。(カテゴリごとに配色やヘッダー画像を変更したいからです)

これどうやってするの?で指定されているテンプレートを変更できますか?

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

これはproduct.viewsにありますか?

ありがとう


これは現在私のcategory_viewであり、http500エラーと無効な構文のpythondjangoエラーを返します。

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

テンプレートを関数パラメーターとして渡すことは、さまざまなテンプレートをビューに渡すことができるためのサッチモスの方法です。ただし、いつでもオーバーライドできます。こちらのドキュメントを詳しく見てください: http://docs.djangoproject.com/en/1.2/topics/http/views/

于 2011-02-26T10:03:30.353 に答える