Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

other_dict must be a mapping (dictionary-like) object

I have the following context processor:

def btc_price(request):
    price = get_price()
    return {'btc_price', price}

In case it matters, here is my get_price function:

def get_price():
    now = datetime.datetime.now()
    if PriceCache.objects.all().exists():
        last_fetch = PriceCache.objects.latest('time_fetched')
        time_last_fetched = last_fetch.time_fetched
        day = datetime.timedelta(days=1)

        if now - time_last_fetched > day:
            api_url = urllib2.Request("https://www.bitstamp.net/api/ticker/")
            opener = urllib2.build_opener()
            f = opener.open(api_url)
            fetched_json = json.loads(f.read())
            cost_of_btc = fetched_json['last']

            PriceCache.objects.create(price=cost_of_btc, time_fetched=now)
            last_fetch.delete()
            return cost_of_btc
        else:
            return last_fetch.price
    else:
        api_url = urllib2.Request("https://www.bitstamp.net/api/ticker/")
        opener = urllib2.build_opener()
        f = opener.open(api_url)
        fetched_json = json.loads(f.read())
        cost_of_btc = fetched_json['last']

        PriceCache.objects.create(price=cost_of_btc, time_fetched=now)
    return cost_of_btc

I have declared the context processor in TEMPLATE_CONTEXT_PROCESSORS, which looks like:

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
"Computer_store.processors.btc_price",
)

And a request context is properly defined in my render_to_response function.

return render_to_response('home.html', {}, context_instance = RequestContext(request))

However, when I try to run this code on the homepage, I get a strange error. TypeError at / other_dict must be a mapping (dictionary-like) object.

A full traceback is available here.

like image 761
Jaxkr Avatar asked Dec 08 '22 15:12

Jaxkr


1 Answers

It looks like

return {'btc_price', price}

should become:

return {'btc_price': price}
like image 181
Scott Woodall Avatar answered Dec 11 '22 08:12

Scott Woodall