Django URL page parameter and another parameter -
how can fix this? in template have this:
<a href="{% url 'hp:category-filtered-page-blog' category=category page=page_obj.next_page_number %}">next</a>
i pagination url parameter. cbv:
class categoryfilteredbloglist(listview): template_name = 'blog.html' context_object_name = 'posts' paginate_by = 1 allow_empty = true def dispatch(self, *args, **kwargs): log_visitor(self.request, 'blog sort on category: {0}'.format(self.kwargs['category'])) return super(categoryfilteredbloglist, self).dispatch(*args, **kwargs) def get_queryset(self): print(self.kwargs['category']) self.category = get_object_or_404(category, title=self.kwargs['category']) return post.objects.filter(category__title__contains=self.category).order_by('-id') def get_context_data(self, **kwargs): context = super(categoryfilteredbloglist, self).get_context_data(**kwargs) context['category'] = self.category return context
my url config:
url(r'^blog/category/(?p<category>.+)$', categoryfilteredbloglist.as_view(), name='category-filtered-blog'), url(r'^blog/category/(?p<category>.+)/page/(?p<page>\w+)$', categoryfilteredbloglist.as_view(), name='category-filtered-page-blog'),
it prints: 'stuff/page/2' 'stuff'.
the problem first url pattern matching including slash itself:
url(r'^blog/category/(?p<category>.+)$', categoryfilteredbloglist.as_view(), name='category-filtered-blog'),
demo of happening:
>>> import re >>> pattern = re.compile('^blog/category/(?p<category>.+)$') >>> pattern.match('blog/category/stuff/page/2').group(1) 'stuff/page/2'
to fix it, remove first url pattern , let second 1 capture category
.
demo:
>>> pattern = re.compile('^blog/category/(?p<category>.+)/page/(?p<page>\w+)$') >>> pattern.match('blog/category/stuff/page/2').group(1) 'stuff'
if want page optional , allow urls blog/category/stuff
work, can swap urls first check page there:
url(r'^blog/category/(?p<category>.+)/page/(?p<page>\w+)$', categoryfilteredbloglist.as_view(), name='category-filtered-page-blog'), url(r'^blog/category/(?p<category>.+)$', categoryfilteredbloglist.as_view(), name='category-filtered-blog'),
Comments
Post a Comment