django-endless with class based views example

2024/9/23 23:22:49

I'm using Class Based Views for the first time. I'm having trouble understating how using class based views I would implement django-endless-pagination twitter styling paging.

Could I have an example of how one would go about this?

This is my view:

class EntryDetail(DetailView):"""Render a "detail" view of an object.By default this is a model instance looked up from `self.queryset`, but theview will support display of *any* object by overriding `self.get_object()`."""context_object_name = 'entry'template_name = "blog/entry.html"slug_field = 'slug'slug_url_kwarg = 'slug'def get_object(self, query_set=None):"""Returns the object the view is displaying.By default this requires `self.queryset` and a `pk` or `slug` argumentin the URLconf, but subclasses can override this to return any object."""slug = self.kwargs.get(self.slug_url_kwarg, None)return get_object_or_404(Entry, slug=slug)
Answer

Since this is a broad question, I would like to combine several solutions for pagination now.

1.Use the generic ListView:

from django.views.generic import ListViewclass EntryList(ListView):model = Entrytemplate_name = 'blog/entry_list.html'context_object_name = 'entry_list'paginate_by = 10

It would be way faster using only urls.py:

url(r'^entries/$', ListView.as_view(model=Entry, paginate_by=10))

So basically you don't need django-endless-pagination in this solution. You can check the example of template here: How do I use pagination with Django class based generic ListViews?

2.Use django-endless-pagination's AjaxListView:

from endless_pagination.views import AjaxListView    
class EntryList(AjaxListView):model = Entrycontext_object_name = 'entry_list'page_template = 'entry.html'

Or faster (again) with urls.py only:

from endless_pagination.views import AjaxListViewurl(r'^entries/$', AjaxListView.as_view(model=Entry))

Reference: http://django-endless-pagination.readthedocs.org/en/latest/generic_views.html

If anyone knows different solution, please comment.

https://en.xdnf.cn/q/71769.html

Related Q&A

Converting adjectives and adverbs to their noun forms

I am experimenting with word sense disambiguation using wordnet for my project. As a part of the project, I would like to convert a derived adjective or an adverb form to its root noun form.For exampl…

Sending a packet over physical loopback in scapy

Ive recently discovered Scapy & it looks wonderfulIm trying to look at simple traffic over a physical loopback module / stub on my NIC.But Scapy sniff doesnt give anythingWhat Im doing to send a pa…

Python Perfect Numbers

So I am supposed to write a Python program that will identify and print all the perfect numbers in some closed interval [ 2, n ], one per line. We only have to use nested while loops/ if-else statement…

Counting consecutive 1s in NumPy array

[1, 1, 1, 0, 0, 0, 1, 1, 0, 0]I have a NumPy array consisting of 0s and 1s like above. How can I add all consecutive 1s like below? Any time I encounter a 0, I reset.[1, 2, 3, 0, 0, 0, 1, 2, 0, 0]I ca…

python 3 replacement for dircache?

Before I go reinventing the wheel, can anyone tell me if theres a drop-in (or semi-drop-in) replacement for the single-line statement:allfiles = dircache.listdir(.)

AES_128_CTR encryption by openssl and PyCrypto

Wondering the right way to convert a AES_128_CTR encryption by openssl to PyCrypto.First, I did an encryption by openssl as following:openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7…

How can i determine the exact size of a type used by python

>>> sys.getsizeof(int) 436 #? does this mean int occupies 436 bytes .>>> sys.getsizeof(1) 12 #12 bytes for int object, is this the memory requirement.I thought int in python is repre…

Python list.clear complexity [duplicate]

This question already has answers here:Python list.clear() time and space complexity?(4 answers)Closed 2 years ago.What is the complexity of the Python 3 method list.clear() ?It is not given here: ht…

Unresolved import org.python / working with jython and java?

Im using Eclipse and I"m trying to create a java program that can run my python code. Im following the guidelines on this page: http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaInt…

elegant unpacking variable-length tuples

A real, if silly problem:https://github.com/joshmarshall/tornadorpc/blob/master/tornadorpc/base.pydef start_server(handlers, ...):...for (route, handler) in handlers:...Normally "handlers" is…