picomon
picomon

Reputation: 1519

Django-Pagination Error

I'm using django-pagination package to paginate users tweets in tweepy. After following all the neccessary instruction, the pagination seems not to work. I guess I'm missing something. Don't know which variable to use in order to replace "object_list". How can I get to make it paginate?

Error:

       TemplateSyntaxError at /test/

       Caught VariableDoesNotExist while rendering: Failed lookup for key [object_list] in u'<tweepy.cursor.ItemIterator object at 0x0437DC10>'

        Request Method:     GET
        Request URL:    http://127.0.0.1:8000/test/
        Django Version:     1.3.1
        Exception Type:     TemplateSyntaxError
        Exception Value:    

      Caught VariableDoesNotExist while rendering: Failed lookup for key [object_list] in u'<tweepy.cursor.ItemIterator object at 0x0437DC10>'

       Exception Location:  C:\Python27\lib\site-packages\django\template\base.py in _resolve_lookup, line 692
       Python Executable:   C:\Python27\python.exe
       Python Version:  2.7.2

Template:

     {% extends "base.html" %}

     {% load pagination_tags %} 

       {% block content %}

        {% autopaginate treats.object_list 10 %} 

          {% for treat in treats %}  

              <center> <p>  {{ treat.author.screen_name}} </p>
         <p>  {{ treat.text|safe }}  {{ treat.url }} </p>
         <p> {{ treat.created_at }}  Via: {{treat.source}} </p> </center>

         {% empty %} 

            <p> Sorry, No Update Could Be Loaded At This Time. </p>
        {% endfor %} 

         {% paginate %} 
          {% endblock %}

Views:

   def testdat(request):
       if request.method=="GET":
          treats=Cursor(api.list_timeline, owner=request.user, slug="").items(20)
          for treat in treats:
              treat.__getstate__()
              top=Twet(text= treat.text, author_screen_name= treat.author.screen_name, created_at= treat.created_at, source= treat.source)
              top.save()
          treats=Cursor(api.list_timeline, owner=request.user, slug='').items(20)
          return render_to_response('dashy.html',{'treats': treats},context_instance=Requ

Upvotes: 0

Views: 1653

Answers (3)

Burhan Khalid
Burhan Khalid

Reputation: 174614

Well the basic problem is that what you are passing to the paginate tag isn't an iterable (like a list or a queryset) so it cannot step through it to paginate.

You can solve this problem by modifying your return return render_to_response('dashy.html',{'treats': list(treats)}

But, tweepy already includes pagination so you can use it out of the box. Your treats object has the following methods .next(), .prev(), .current_page(), .count(), .page_iterator(), .page_index() which you can use to implement pagination directly.

See this link for a diagram of all the methods that ItemIterator exposes.

Upvotes: 1

Steve K
Steve K

Reputation: 11369

I assume your Python code uses iterator() to return the QuerySet, in which case this would make sense, since the queryset is iterable but has no length. Maybe you should remove iterator() from the code returning treats if it is present, or at least make a simple query.

treats = Treat.objects.filter(price__gt=100)
...
return {'treats':treats}

Then your template is wrong, as Krzystof stated, just do

{% autopaginate treats 10 %}

Because I think when you loop over the treats in yout template, you have to paginate the exact same iterable variable.

Upvotes: 0

Krzysztof Rosiński
Krzysztof Rosiński

Reputation: 1478

Try:

    {% autopaginate treats 10 %} 

A query set (treats I believe) doest not contain object_list by default

Upvotes: 0

Related Questions