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>First and foremost, you should use a <a href="https://docs.djangoproject.com/en/1.10/ref/models/fields/#decimalfield" rel="nofollow">decimal field</a> for your currency values if you can. This is not a django convention, <a href="http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when#1165788">it's a general programming one</a>; decimal types can hold the necessary precision for a valid "money" representation.</p>

<p>As pointed by <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/humanize/" rel="nofollow">the documentation</a>, you have to install the app <code>humanize</code>, and in your template, load it up with <code>{% load humanize %}</code>. Keep in mind that it <strong>will</strong> use the appropriate locale provided in your <code>settings.py</code>.</p>

<p><a href="https://docs.djangoproject.com/en/1.10/ref/templates/language/#custom-tag-and-filter-libraries" rel="nofollow">Here</a> you have a very simple example using <code>{% load humanize %}</code></p>

python django dictionary type-conversion