【问题标题】:Pagination class based views django 1.5基于分页类的视图django 1.5
【发布时间】:2014-10-12 01:40:04
【问题描述】:

我正在开发一个基于 Django==1.5.7 的网站

我需要在我的应用程序中对表格的ListView 进行分页。

这是我正在使用的代码

在models.py上:

class UsuarioFidetel(models.Model):
"""
Modelo de usuario fidetel
"""

usuario = models.CharField(max_length=30)
id_usuario = models.IntegerField()
nombre = models.CharField(max_length=255, null=True)
apellido = models.CharField(max_length=255, null=True)
tipo_cedula = models.CharField(max_length=1, null=True)
cedula = models.CharField(max_length=9, null=True)
fecha_nacimiento = models.DateField(null=True, blank=True)
sexo = models.CharField(max_length=1, null=True)
correo = models.EmailField(max_length=254, null=True)
estado_civil = models.CharField(max_length=1, null=True)

def __unicode__(self):
    return self.nombre

在views.py上:

class UsarioList(ListView):
model = UsuarioFidetel
template_name = 'all_list.html'

在 urls.py 上:

url(r'^usario/$', UsarioList.as_view(model=UsuarioFidetel, paginate_by=2)),

注意 url 的 paginate_by 属性,这工作得很好,但是当我转到第二页或第一页之外的任何页面时,它会抛出此错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:9001/usario?page=2

一切正常,但我认为这将通过模板块内的页面,没有重大错误,无法弄清楚这里有什么问题,也许我需要在 url 上使用正则表达式?

有什么想法吗?

提前致谢!

【问题讨论】:

    标签: python regex django pagination


    【解决方案1】:

    没关系,在模板上解决了,即:

    <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="/fidetel/usario?page={{ page_obj.previous_page_number }}">&lt;</a>
                {% endif %}
                <span class="page-current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="/fidetel/usario?page={{ page_obj.next_page_number }}">&gt;</a>
                {% endif %}
            </span>
    

    现在它可以正常工作了

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 2013-09-23
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      相关资源
      最近更新 更多