C
C
Cyril2014-05-29 13:57:39
Django
Cyril, 2014-05-29 13:57:39

Why does Django tag output +0 for the base template?

I have a new comment count in radish, created a simple_tag that outputs the number of new comments to the base.html header:

@register.simple_tag()
def get_new_comm(id):
    cacheDB = redis.StrictRedis()
    temp = cacheDB.get("comments:%s" % id)
    if temp:
        return '<font color="red" >+' + temp + '</font>'
    else:
        return ""

The number of new ones displays correctly, if a variable is not created in the radish (there have never been new comments), then it does not display anything, as it should. But if the variable is 0, then writes +0 new comments. Even if I put
if temp!= 0 :
What can I do with it?
And an additional question, I connect a radish in every function
import redis
cacheDB = redis.StrictRedis()

and it seems to me that it creates a new connection every time it takes time. How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Solovyov, 2014-05-29
@aeHarkonnen

Do this, convert the value to an integer
temp = int(cacheDB.get("comments:%s" % id))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question