super9
super9

Reputation: 30111

Why is this django template not working?

This was working 2 weeks ago. I can't figure what is causing this error now. I am getting the error Caught TypeError while rendering: 'int' object is not iterable on the line in my template where the FOR loop starts.

views.py

@csrf_exempt 
def my_status(request):

    import twitter
    client = twitter.Api()

    #get status of user. get last 100 status only 

    if 'twitternick' in request.POST:
        r = request.POST['twitternick']

        tags = client.GetUserTimeline(r, count='10')

        taglist = [s.text for s in tags]

        ## getting the dictionary

        # create list of lower case words, \s+ --> match any whitespace(s)
        word_list = re.split('\s+',str(taglist))

        # create dictionary of word:frequency pairs
        freq_dic = {}

        # punctuation marks to be removed
        punctuation = re.compile(r'[.?!,":;]') 
        for word in word_list:
            # remove punctuation marks
            word = punctuation.sub("", word)
            word = word.replace("u'",'')
            # form dictionary
            try: 
                freq_dic[word] += 1
            except: 
                freq_dic[word] = 1


        # create list of (key, val) tuple pairs
        tags = [(k,v*10) for k,v in freq_dic.items()]
        tags = dict(tags)

    return render_to_response('twitter_app/mystatus.html', {'latest_status': tags}

data structure for tags

{'PAP': 10, 'btw': 10, 'via': 20, 'crowd': 10, 'is': 10, 'half': 10, 'anyway': 10, "#fb'": 10, 'items': 10, '[@leynaaaa': 10, 'are': 10}

template

{% extends "twitter_app/base.html" %}

{% block content %}
<h2>Here comes your status:</h2>

    {% for k, v in latest_status.items %}

    <span style="font-size:{{ v }}px;">{{ k }}</span>

    {% endfor %}

{% endblock %}

Upvotes: 1

Views: 1331

Answers (2)

Thierry Lam
Thierry Lam

Reputation: 46264

In your data:

{'PAP': 10, 'btw': 10, 'via': 20, 'crowd': 10, 'is': 10, 'half': 10, 'anyway': 10, "#fb'": 10, 'items': 10, '[@leynaaaa': 10, 'are': 10}

There's a key called items. Can you pass in a test dictionary without the items key to the template to see if it works?

Workaround:

Assuming you can't rename the items key, you can pass in {'latest_status': tags.items()} from your view. In the template, do a for loop on {% for k, v in latest_status %}.

Upvotes: 2

bdd
bdd

Reputation: 3434

latest_status.items is returning a single integer, not an interable data-structure.

Log the value of latest_status.items and tell me what you get.

Upvotes: 0

Related Questions