Home  >  Q&A  >  body text

python - django-pagination分页url显示方式的问题

django-pagination分页url默认显示方式,例如?page=2 可以改成/page/2的形式么?

巴扎黑巴扎黑2728 days ago803

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 14:37:43

    The scenarios for these two usages are different

    ?page=2
    The page=2 here is passed as url parameters. You can get it in request.kwargs in Django view. It is generally used to pass ordinary parameters:
    request.kwargs['page'] # 2

    /page/2/
    This "2" is the ontology of the url. This usage is generally used with Django class-based view to automatically parse the Django model object corresponding to this "2"
    For example url.py:

    url(r'^user/(?P<pk>\d+)/edit/$', views.YourView.as_view(), name='your_view')
    

    views.py:

    from django.views.generic import DetailView
    class YourView(DetailView):
        def get_object(self, queryset=None):
            pk = self.kwargs.get(self.pk_url_kwarg, None)
            if int(pk) == 0:
                return None
            return super(Yourview, self).get_object(queryset)
    

    The get_object method here does something similar, so that you can access the object directly through self.object
    You can refer to the official documentation
    https://docs.djangoproject.com/en/1.7/topics/class-based-views/generic-display/

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:37:43

    It feels like it has nothing to do with the paging class. It’s just a question of how to pass the page parameter. It can be passed through URLConf or created through QueryString. After getting this parameter, use pagination to implement paging

    reply
    0
  • Cancelreply