gglasses gglasses - 6 months ago 43
Python Question

Django: Make pagination's urls SEO friendly

I'm trying to make my pagination url's from

django
a little bit SEO friendly. Instead of
?page=current_page
something of the form
/page/current_page
.

So in my
app/urls.py
I did the following:

url(r'^(?P<slug>[a-zA-Z0-9-_]+)/page/(?P<page>[0-9])+$', GalleryDetail.as_view(), name='galleries-view-gallery-paginator')


and on my
app/templates/app/my_view.html
:

{% if page_obj.has_next %}
<a href="{% url 'galleries-view-gallery-paginator' page_obj.next_page_number %}">next</a>
{% endif %}


But I get a
NoReverseMatch
error.

Other error info:
Reverse for 'galleries-view-gallery-paginator' with arguments '(2,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['gallery/(?P<slug>[a-zA-Z0-9-_]+)/page/(?P<page>[0-9])+$']


And well. How can I achieve urls like
/page/current_page
using
Django
?

Answer

URL galleries-view-gallery-paginator requires you to pass 2 parameters: slug and page. Since you are passing only the page number, slug is also needed:

{% url 'galleries-view-gallery-paginator' gallery.slug page_obj.next_page_number %}
Comments