slny06
slny06

Reputation: 67

Calling method of object in Django template

Having trouble calling methods of non-Django classes in django template.

I've tried both calling it as an attribute and creating a custom filter of which neither I can get to return anything.

Apologies if this is obvious but I'm not well versed in Django and I can't seem to find any reference of non Django model classes in their documentation

I have a class

class MarketDataSet():
    def __init__(self, market_data):
        self.market_data = market_data

    def filter_data(self, side):
        filtered = list()
        for dp in self.market_data:
            if dp.side == side:
                filtered.append(dp)
        return filtered

    def bids(self):
        return self.filter_data(side='bid')

    def asks(self):
        return self.filter_data(side='bid')

    def trades(self):
        return self.filter_data(side='trade')

    def high_bid(self):
        amounts = list()
        if not self.bids():
            return ''
        else:
            for bid in self.bids():
                amounts.append(bid.amount)
            return max(amounts)

    def low_ask(self):
        amounts = list()
        if not self.asks():
            return ''
        else:
            for ask in self.asks():
                amounts.append(ask.amount)
            return max(amounts)

    def average_trade(self):
        amounts = list()
        if not self.trades():
            return ''
        else:
            for trade in self.trades():
                amounts.append(trade.amount)
            return statistics.mean(amounts)

and views.py (relevant portion):

    if form.is_valid():
        style_id = form.get_style().upper()
        stats = dict()
        st_data = MarketDataSet(market_data=get_full_market_data(style_id=style_id, size=None))
        stats.update({'st': st_data})
        return render(request=request,
                      template_name='main/pricer_response.html',
                      context={'form': form,
                               'stats': stats,
                               })

and pricer_response.html template (relevant portion) Notice both approaches:

{% for platform in stats.keys %}
    {% block content %}
    {% load static %}
                    <tr>
                        <td><b>{{platform.title}}</b></td>
                        <td>{{stats.platform|get_method:high_bid}}</td>
                        <td>{{stats.platform.low_ask}}</td>
                    {% endfor %}
{% endblock %}

and custom_tags.py:

@register.filter(name='get_method')
def get_method(obj, method_name):
    return getattr(obj, method_name)

Upvotes: 2

Views: 1483

Answers (2)

slny06
slny06

Reputation: 67

This was more of a misunderstanding as how to access dictionary items in django templates. Solution below:

            {% for platform, dataset in stats.items %}
            <tr>
                <td><b>{{ platform.title }}</b></td>
                <td>{{ dataset.high_bid }}</td>
                <td>{{ dataset.low_ask }}</td>

Upvotes: 1

ufe
ufe

Reputation: 81

Inside your for loop, try accessing the method like so

{{ stats[platform].method_to_call }}

Upvotes: 1

Related Questions