Custom category views, urls & templates: Part 1

3 views
Skip to first unread message

Christopher Hart

unread,
Jun 26, 2009, 1:32:19 AM6/26/09
to Satchmo users
We have a store and need to have different promotional stuff show up for each category's view. Since there is only one base_category.html template, we need to create templates for each of our main categories that extend the base.

In order to have the category-specific templates actually used it seems like we need to write views that are otherwise identical to category_view, except with the parameter template='product/detail_mycategory.html'.

Can we just make a product/views.py in the project folder that looks something like this?:

##############################

from satchmo.product import views

def mycategory_view(request, slug, parent_slugs='', template='base_category.html'):
    """Display the category, its child categories, and its products.

    Parameters:
     - slug: slug of category
     - parent_slugs: ignored   
    """
    try:
        category = Category.objects.get(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,
    }
    index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products)
    return render_to_response(template, RequestContext(request, ctx))

##############################

...and then add to the project's urls.py something like this? (for each custom category of course):

##############################

(r'^category/mycategory/$', 'mycategory_view', {}, 'satchmo_category'),

##############################

...and finally just extend the templates as usual by creating product/detail_mycategory.html.

If these suspicions are correct we will go ahead and implement them. Then Part 2 would be to create a couple custom models to hold banner images with associated links to categories or products that the client can manipulate through the admin interface. And then simply put stuff like <a href="{{ prouct_or_category_url }}"><img src="{{ media_url }}/images/{{ storename.bannername.path }}" /></a> etc. into the templates we created in Part 1 of this exploration.

Thanks so much for any replies.




Reply all
Reply to author
Forward
0 new messages