Satchmo,想要根据选择的产品类别更改模板

发布于 2024-10-19 05:49:33 字数 1468 浏览 7 评论 0原文

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

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

我该怎么做?我可以更改在product.views 中指定的模板吗

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

谢谢


这是我当前的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))

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-10-26 05:49:33

如果您查看 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 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文