110
114
return HttpResponseRedirect(self.get_next_url())
117
class PaginatedListView(ListView):
118
"""Paginating extension to :class:`django.views.generic.ListView`
120
Adds to the normal list view pagination support by including context
121
variables for relative links to other pages, correctly preserving the
122
existing query string and path.
127
def _make_page_link(self, page_number):
128
"""Gives relative url reference to `page_number` from current page
130
The return will be one of:
131
- A query string including the page number and other params
132
- The final path segment if there are no params
133
- '.' if there are no params and there is no final path segment
135
See RFCs 1808 and 3986 for relative url resolution rules.
137
The page number is not checked for sanity, pass only valid pages.
139
new_query = self.request.GET.copy()
141
if "page" in new_query:
142
del new_query["page"]
144
new_query["page"] = str(page_number)
146
return self.request.path.rsplit("/", 1)[-1] or "."
147
return "?" + new_query.urlencode()
149
def get_context_data(self, **kwargs):
150
"""Gives context data also populated with page links
152
If already on the first or last page, the same-document reference will
153
be given for relative links in that direction, which may be safely
154
replaced in the template with a non-anchor element.
156
context = super(PaginatedListView, self).get_context_data(**kwargs)
157
page_obj = context["page_obj"]
158
if page_obj.has_previous():
159
context["first_page_link"] = self._make_page_link(1)
160
context["previous_page_link"] = self._make_page_link(
161
page_obj.previous_page_number())
163
context["first_page_link"] = context["previous_page_link"] = ""
164
if page_obj.has_next():
165
context["next_page_link"] = self._make_page_link(
166
page_obj.next_page_number())
167
context["last_page_link"] = self._make_page_link(
168
page_obj.paginator.num_pages)
170
context["next_page_link"] = context["last_page_link"] = ""
113
174
def process_form(request, form_class, redirect_url, prefix,
114
175
success_message=None, form_kwargs=None):
115
176
"""Utility method to process subforms (i.e. forms with a prefix).