Hi,
I’m working with objects from the ObjetArchi
model. When I navigate on the page of an object, based on a DetailView
generic class based view, I’d like to integrate “Previous object” and “Next object” links that open the page of the previous (or the next) object, based on the id
field, by order of creation.
Some ids are missing in the database, but the ascending order is preserved. e.g.: 13, 22, 23, 24, 25, 35, 56, etc.
If object.id +1 (or -1) doesn’t exist, it should iterate until the next (or previous) existing id.
How could I implement this?
Thanks for your hints!
views.py
:
class Notice(DetailView):
model = ObjetArchi
template_name = 'notices/objetarchi_detail.html'
urls.py
:
from django.urls import path
from notices import views
from django.contrib.flatpages import views as flatviews
app_name = 'notices'
urlpatterns = [
path('', views.NoticesListView.as_view(), name='notices_list'),
path('notices', views.NoticesListView.as_view(), name='notices'),
path('notices/<int:pk>', views.Notice.as_view(), name='detail'),
path('notices/<int:pk>/pdf', views.NoticePdf.as_view(), name='detail_pdf'),
path('apropos', flatviews.flatpage, {"url": "/apropos/"}, name='apropos'),
]