3

I have a dictionary in my views.py file where the values are lists. In my template I have the following html:

{% for key, values in my_dict %}
    <tr>
        <td colspan="6" class="key">{{ key }}</td>
    </tr>
    {% for value in values %}
        <tr>
            <td colspan="6" class="value">{{ value }}</td>
        </tr>
    {% endfor %}
{% endfor %}

which prints out the name of the key followed by the list items inside the value. However, I want to be able to paginate the keys such that only 10 keys with their values appear per page.

I have the following code to do the pagination inside my index method in views.py:

paginator = Paginator(myDict, 10)
page_num = requests.GET.get('page', 1)
page = paginator.page(page_num)

I updated my template to {% for key, values in page %} and of course I get a TypeError because it's a hashable type. I'm just wondering how I can go about producing the same results as before but without using a dictionary.

I found this answer that suggests using tuples instead of dictionaries, but that doesn't seem to work for me and I'm guessing because my values are lists.

Community
  • 1
  • 1
steveclark
  • 537
  • 9
  • 27
  • Do you have the lists as keys or values? You have mentioned both situations (at the beginning and at the end). – bellum May 21 '15 at 19:44
  • Whoops my bad I'll fix it now. The values are lists and the keys are single objects. It's a test suite object mapping to its list of test case objects if that's relevant at all. – steveclark May 21 '15 at 19:53

1 Answers1

6

I am not sure if it can be count as good answer but anyway: I have just tried to replicate your problem and solution with tuple and it worked. So I think the problem can be in your code. My test code:

>>> from django.core.paginator import Paginator
>>> d = {'a': [1,2], 'b': [3,4]}
>>> p = Paginator(d, 1)
>>> p1 = p.page(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type
>>> t = tuple(d.items())
>>> t
(('a', [1, 2]), ('b', [3, 4]))
>>> p = Paginator(t, 1)
>>> p.page(1).object_list
(('a', [1, 2]),)
>>> p.page(2).object_list
(('b', [3, 4]),)

UPDATE: and looping also works. So can you show what structures do you have?

bellum
  • 3,642
  • 1
  • 17
  • 22
  • It is definitely my code. I had `tuple(d)` not `tuple(d.items())` and I was also using `t[1]` instead of `t(1)`. I'll see if I can get it working using your answer and let you know. – steveclark May 21 '15 at 20:05
  • @steveclark `tuple(d)` gives you `('a', 'b')` so just keys. – bellum May 21 '15 at 20:14
  • Yeah I found that out the hard way haha. Anyways your solution worked. Also, if anyone finds themselves here in your template you need to use `{% p.page(1).object_list.0 %}` to access the key (.1 for the value). Indexing using `[i]` does not work. – steveclark May 21 '15 at 20:20
  • @steveclark Actually brackets don't work in template too, but I guess you meant `{{ page_object.object_list.0 }}`. Also you can skip `object_list` and just write `{{ page_object.0.0 }}` and `{{ page_object.0.1 }}`. – bellum May 21 '15 at 20:25