Displaying featured items

18 views
Skip to first unread message

Neal Blackburn

unread,
Apr 30, 2008, 10:09:41 AM4/30/08
to Satchmo users
Hey guys, for my front page, the get_featured function like shown
here, http://www.satchmoproject.com/trac/browser/satchmo/trunk/satchmo/product/views.py#L260
would be perfect. My problem arises in that I would like to make use
of the arguments that it looks like i can use, but don't know how. for
instance if you look at that function, apparently i can use
num_to_display to say i just want 4 of the featured items shown? how
can I call that on a template?

Also how can i count in a loop on the template or is that possible,
like after the 2nd i want to insert a pice of code (for instance a new
row in a html table or whatever).

thanks everyone, i can explain more if necessary.

nb

Bruce Kroeze

unread,
Apr 30, 2008, 11:45:08 AM4/30/08
to satchm...@googlegroups.com
Neal,

To set the number of featured, go to http://yourshop/settings/  and adjust the number for ""Number of featured items to display" setting.

To count in a loop, you can do something like this:

{% for product in all_products_list %}
<div class = "productImage">    
           {% ifequal forloop.counter0 3 %}
                <!-- here you add your code to do on the fourth item -->
           {% endifequal %}
           {% if product.main_image %}
              <center>
              <a href="{{ product.get_absolute_url }}"><img src="{{ product.main_image.get_picture_url|thumbnail:"width=85" }}" width="85" /> </a>
              </center>
           {% endif %}     
            <p><a href="{{ product.get_absolute_url }}">{{ product.translated_name }}</a></p>
        </div>
{% endfor %}

If you want to do something every fourth row, rather than just on the first fourth, then you could use a tag I just added for you in rev 1169, I use it for my own clients all the time.

Instead of:
{% ifequal forloop.counter0 3 %}

you would load satchmo_utils and use:
{% if forloop.counter0|is_mod:4 %}

Enjoy,
Bruce Kroeze
http://solidsitesolutions.com
Dynamic Designs, Optimized



Neal Blackburn

unread,
Apr 30, 2008, 12:29:33 PM4/30/08
to Satchmo users
Alright, thanks Bruce, this is looking good. Now is there a way to
pull featured products on the category page? like /category/cakes/ i
want to run your loop on it pulling just featured items that belong to
cakes... make sense?

Take care,
Neal Blackburn

On Apr 30, 11:45 am, "Bruce Kroeze" <bkro...@gmail.com> wrote:
> Neal,
> To set the number of featured, go tohttp://yourshop/settings/ and adjust

Costantino Giuliodori

unread,
Apr 30, 2008, 1:35:37 PM4/30/08
to satchm...@googlegroups.com


2008/4/30 Neal Blackburn <cyber...@gmail.com>:


Alright, thanks Bruce, this is looking good. Now is there a way to
pull featured products on the category page? like /category/cakes/ i
want to run your loop on it pulling just featured items that belong to
cakes... make sense?

Hi,
I have do something like this in shop/views/category.py

[...]
        products = Product.objects.active()
        subcategories = category.get_all_children()
        if subcategories:
            q = Q( category=subcategories[0] )
            if len( subcategories ) > 1:
                for c in subcategories[1:]:
                    q = q | Q( category=c )
            products = products.filter( q )
            number_subcategory_products = products.count()
            products = products.order_by('?')[:ppp]
            results = { 'category': category , 'products': products , 'subcategory': 1 , 'number_subcategory_products' : number_subcategory_products , 'number_subcategory': len( subcategories )}
        else:
            results = { 'category': category , }


Bruce Kroeze

unread,
Apr 30, 2008, 1:49:14 PM4/30/08
to satchm...@googlegroups.com
Neal, I think you should make a custom view similar to that proposed by Constantino.  The only modification would be to add "featured=True" to his query.
--

Neal Blackburn

unread,
May 1, 2008, 2:23:05 PM5/1/08
to Satchmo users
I found that if i use the original product pull:

{% for product in category.product_set.active %}
{% if forloop.first %} <ul class="sub-nav"> {% endif %}
<li><a
href="{{ product.get_absolute_url }}">{{ product.translated_name }}</
a> {% if product.featured %}Featured{% endif %}</li>
{% if forloop.last %} </ul> {% endif %}
{% endfor %}

Note the: {% if product.featured %}Featured{% endif %}
that actualy works. but what i really need, as i have a more complex
for loop is to be able to pull just the featured, is it possible to
rework the orginal statement:
{% for product in category.product_set.active %} into something
like:
{% for product.featured in category.product_set.active %} ?

Grant it, I've already tried the exact above and not successful, but
if there is another way, as I'm trying to avoid
Costantino's suggestion, mainly because I don't want to change the
Satchmo code, and also, I really don't know how I'd get that info to
my template once added to the views.py file.

Thanks everyone with your help!

nb



On Apr 30, 1:49 pm, "Bruce Kroeze" <bkro...@gmail.com> wrote:
> Neal, I think you should make a custom view similar to that proposed by
> Constantino. The only modification would be to add "featured=True" to his
> query.
>
> On Wed, Apr 30, 2008 at 10:35 AM, Costantino Giuliodori <
>
>
>
> costantino.giuliod...@gmail.com> wrote:
>
> > 2008/4/30 Neal Blackburn <cybernet...@gmail.com>:
>
> > > Alright, thanks Bruce, this is looking good. Now is there a way to
> > > pull featured products on the category page? like /category/cakes/ i
> > > want to run your loop on it pulling just featured items that belong to
> > > cakes... make sense?
>
> > Hi,
> > I have do something like this in shop/views/category.py
>
> > [...]
> > products = Product.objects.active()
> > subcategories = category.get_all_children()
> > if subcategories:
> > q = Q( category=subcategories[0] )
> > if len( subcategories ) > 1:
> > for c in subcategories[1:]:
> > q = q | Q( category=c )
> > products = products.filter( q )
> > number_subcategory_products = products.count()
> > products = products.order_by('?')[:ppp]
> > results = { 'category': category , 'products': products ,
> > 'subcategory': 1 , 'number_subcategory_products' :
> > number_subcategory_products , 'number_subcategory': len( subcategories )}
> > else:
> > results = { 'category': category , }
>
> --

Neal Blackburn

unread,
May 13, 2008, 1:00:37 PM5/13/08
to Satchmo users
Costantino,

I am going to try your solution, but I am a total newb to this stuff,
only now am I barely grasping the concepts of views and their
intergration with the template. so lets say i use your block of code,
with the "featured=True" that Bruce talks about. comming from a php
background, i would specify a function, then call it in the template/
page. here i'm not sure what to do, do i define something then how do
i get it to display correctly on my template.

Thanks to all the help while I'm learning.

Neal Blackburn


On Apr 30, 1:35 pm, "Costantino Giuliodori"
<costantino.giuliod...@gmail.com> wrote:
> 2008/4/30 Neal Blackburn <cybernet...@gmail.com>:
>
>
>

Costantino Giuliodori

unread,
May 14, 2008, 12:21:28 PM5/14/08
to satchm...@googlegroups.com
Hi Neal,
I'm a newb too.

There is no need to use a function.
You can simply iterare products returned from the view with a cycle like this (what is important is that I replace the block product_list part of the page):

{% block product_list %}
{% for product in products %}
    {% if forloop.first %}{% endif %}
      {# {% if product.main_image.get_picture_url %} #}
        <table>
            <tr>
                <td class="boxImmagineProdotto">
                    <div class="imgprod"><a href="{{ product.get_absolute_url }}"><img src="{{ product.main_image.get_picture_url|thumbnail:"width=75,height=66" }}" alt="{{ product.translated_name }}" /></a></div>
                </td>
                <td class="titoloProdotto">
                    <a href="{{ product.get_absolute_url }}">{{ product.translated_name }}</a>
                    {% if only_subcategory %} <div class="attributiProdotto">{% trans "Category" %}: <a href="{{product.get_category.get_absolute_url}}">{{product.get_category.name}}</a></div> {% endif %}
                </td>
            </tr>
            <tr>
                <td class="descrizioneBreveProdottoInLista">{{ product.translated_short_description }}</td>
                <td class="prezzoOffertaInLista">{{product.unit_price|currency}}</td>
            </tr>
        </table>           
    {% endif %}
{% endfor %}
{% endblock %}

if the view return a products list in the dictionary (result), the template will iterate them, compiling a list of products in a table, otherwise (if not subcategories) they will not appear.
All is prepared from the view and drawn from the template.

2008/5/13 Neal Blackburn <cyber...@gmail.com>:

Costantino Giuliodori

unread,
May 14, 2008, 12:37:36 PM5/14/08
to satchm...@googlegroups.com
An example of an under construction satchmo shop with  random featured products for category is that: http://www.globalelectrics.it/shop/category/antitheft/

Here is where I take the view and template snippets.

2008/5/14 Costantino Giuliodori <costantino...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages