Satchmo,想要根据选择的产品类别更改模板
我的数据库中列出了两个类别。
我想根据选择的类别更改模板(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看 Django 站点上的教程以及文档中的其他位置,您会发现这种处理方式非常容易使用不同的模板:
将模板作为函数参数传递只是 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:
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/