हल: django सूचीदृश्य

सूचीदृश्यों के साथ मुख्य समस्या यह है कि उनका उपयोग करना कठिन होता है और भ्रमित करने वाला हो सकता है।

 with pagination

I am trying to create a listview with pagination in Django. I have tried the following code but it is not working:
<code>class MyListView(ListView):

    model = MyModel
    template_name = 'my_template.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        context['page'] = self.request.GET.get('page')

        return context

    def get(self, request, *args, **kwargs):
        response = super().get(request, *args, **kwargs)

        try:
            page = int(response.context['page']) - 1
            if page &lt; 0: page = 0  # first page is 1 not 0! (paginator bug?)
            response.context['previous'] = str(page) if page &gt; 0 else None  # None for first page! (paginator bug?)
            response.context['next']     = str(page + 2) if len(response.context['object_list']) == 10 else None  # None for last page! (paginator bug?)

        except KeyError: pass  # no 'page' in the context... means we're on the first one! (no previous!) or last one! (no next!) or something went wrong... just ignore it and don't add anything to the context then...

        return response    
</code>

यह कोड पृष्ठांकन के साथ सूचीदृश्य के लिए एक वर्ग-आधारित दृश्य है। पहली चार पंक्तियाँ वर्ग, उपयोग किए जाने वाले मॉडल, उपयोग किए जाने वाले टेम्पलेट और उपयोग किए जाने वाले संदर्भ डेटा को परिभाषित करती हैं। अगली चार पंक्तियाँ प्राप्त विधि और प्रतिक्रिया को परिभाषित करती हैं। अंतिम चार पंक्तियाँ get_context_data विधि को परिभाषित करती हैं और संदर्भ डेटा लौटाती हैं।

लिस्टव्यू क्या है

सूचीदृश्य Django में एक विजेट है जो वस्तुओं की एक सूची प्रदर्शित करता है।

संबंधित पोस्ट:

एक टिप्पणी छोड़ दो