On 29/05/2021 1:28 am, Ken Bolton wrote:


On Fri, May 28, 2021 at 4:56 AM ek ekke <[email protected] <mailto:[email protected]>> wrote:

    I am trying to list all products from cartridge, is that function
    all ready in there and how can i call it from a template?
    Don't want to write the view if it's all ready in there somewhere.


Check out the `Category` module, which is a subclass of Mezzanine's `Page`.

Simplest way is

Product.objects.all()

To get that in a template, you might need to write a template tag, e.g. I added something to my Orders Change List template so that I could filter orders by a specific product (and export the results to CSV). The basics are:

In admin.py (or forms.py):

def get_all_products():
    return [(p.id, p.title) for p in Product.objects.all().order_by('title')]

class OrderByProductForm(forms.Form):
    product = forms.ChoiceField(label="Export filtered orders containing",
                                choices=[])

    def __init__(self, *args, **kwargs):
        super(OrderByProductForm, self).__init__(*args, **kwargs)
        self.fields["product"].widget = forms.Select(choices=get_all_products())

templatetags/myapp_tags.py:

@register.simple_tag
def export_order_by_product_form(request):
    querystring = request.GET.dict()
    return OrderByProductForm(queries=querystring)

And then in templates/admin/order_change_list.html:

{% extends "admin/change_list.html" %}
{% load admin_urls myapp_tags %}

{% block object-tools-items %}

<li><form action="/admin/order_export/" method="get">
   {% export_order_by_product_form request %}
   <input type="submit" value="Export to CSV" />
</form></li>
{{ block.super }}
{% endblock %}

Note then i have also defined a view for "order_export" which uses the selected Form item to find the right orders and export them to CSV.

Hope this helps.

Seeya. Danny.


--
You received this message because you are subscribed to the Google Groups "Mezzanine 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mezzanine-users/08b321ba-261e-dea9-1866-c4d9dcdd84f1%40gmail.com.

Reply via email to