Python/Django convert/humanize specific dictionary items on django view?
<p>I am creating a variable queryset and then passing the values into a dictionary context variable. I am able to convert dates but I'm not sure how to convert specific fields to currency (example: $1,000 instead of 1000) or even just humanize specific fields with commas (1,000 instead of 1000) below is my code from my view, the two methods are a part of a class:</p>

<pre><code>from datetime import date

def get_context_data(self):
    context = super(MyView, self).get_context_data()
    context['myview_items'] = self.get_mymethod_items_context()
    return context

def get_mymethod_items_context(self):
    context = {}
    items = table.objects.values('date_begun', 'price', 'item_number')

    context['items'] = items
    context['headers'] = ['Date', 'Price', 'Item']
    context['fields'] = ['date_begun_date', 'price', 'item_number']
    return context
</code></pre>

<p>I only want to convert one field, which is what I was trying to do for the price field:</p>

<pre><code>from django.contrib.humanize.templatetags.humanize import intcomma 
intcomma('price')
</code></pre>

<p>How im creating tables, Template tag on my template:</p>

<pre><code>{% simple_table_print 'tableid1' 'Price Information' myview_items.items myview_items.fields myview_items.headers %}  
</code></pre>

<p>I would install <code>humanize</code> app and then extend its <code>intcomma</code> method to create my own templatetag:</p>

<pre><code>from django import template
from django.contrib.humanize.templatetags.humanize import intcomma

register = template.Library()

@register.filter
def prepend_dollars(dollars):
    if dollars:
        dollars = round(float(dollars), 2)
        return "$%s%s" % (intcomma(int(dollars)), ("%0.2f" % dollars)[-3:])
    else:
        return ''
</code></pre>

<p>First load your customtags.py file in your template:</p>

<pre><code>{% load customtags %}
</code></pre>

<p>You can then use it in your template:</p>

<pre><code>{{ my_decimal_val | prepend_dollars }}
</code></pre>

<p>Hope it helps.</p>

python django dictionary type-conversion