简体   繁体   中英

How to show Redis sorted set elements in a webpage

I'm working on a project which simulates a simple twitter-like social media using Redis as DB, and it includes python-to handle redis- and django framework. I have a function which is suppose to return the last 30 posts of a person's timeline, which goes like this:

def get_status_messages(conn, uid, timeline='home:', page=1, count=30):
    statuses = conn.zrevrange('%s%s'%(timeline, uid), (page-1)*count, page*count-1)
    pipeline = conn.pipeline(True)
    for id in statuses:
        pipeline.hgetall('status:%s'%id)
    return filter(None, pipeline.execute())

the list of timeline posts is stored in a sorted set which saves post id and post time stamp and sorts the list by the latter. And each status post is saved as a hash which has a unique id. timeline zset has the name 'profile:xxx' with xxx being the author's id, and each post has the name 'status:yyy' with yyy being the post's unique id. Im trying to show these posts in a html page, and here goes my home 'view' which represents timeline:

def home(request):
    id = request.session.get('member_id')
    prof=get_status_messages(conn, id, timeline='home:', page=1, count=30)
    fp = open('template/home.html')
    t = Template(fp.read())
    fp.close()
    html = t.render(Context({'item_list': prof.iteritems()}))
    return HttpResponse(html)

And finally in the html file for timeline, we have this:

<html>
<head>
    <title>homePage</title>
</head>
<body>
<ol>
{% for key, value in item_list %}
    <li>{{ key }} : {{ value }} </li>
{% endfor %}
</ol>

</body>
</head>
</html>

However, when I proceed to timeline page, I have this error displayed:

'list' object has no attribute 'iteritems'

I have used the very same pattern for reading items in a hash, and it worked just fine. Since I'm a newbie in python and django, Im not really sure what could be the problem and why it doesn't work whith zset. Does anyone have any idea what could be the problem here?

Edit: I tried to print value of 'prof' variable, and this is the result. Note that 'hello world','I am pegah kiaei!' and 'rrr' are test tweets from a following user:

[{'uid': '2', 'login': 'p_kiaei', 'id': '7', 'message': '\trrr', 'posted': '1492107986.573'},
 {'uid': '2', 'login': 'p_kiaei', 'id': '6', 'message': '\tI am pegah kiaei!', 'posted': '1492107752.173'},
 {'uid': '2', 'login': 'p_kiaei', 'id': '5', 'message': 'hello world\t', 'posted': '1492107393.602'}] .

Thanks in advance!

Each result in the pipeline is a dict. But the pipeline itself returns a list; just pass prof directly to the context.

Edit So, you also need to add an extra loop in the template:

<ol>
{% for item in item_list %}
  {% for key, value in item %}
      <li>{{ key }} : {{ value }} </li>
  {% endfor %}
{% endfor %}
</ol>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM