Django views.py updates pagination from category selection in class mode

The problem is a bit complicated. In fact, I'm not trying to reinvent the wheel, and since my main developer came out, I try not to destroy its code.

But I think that this time I will need to change a lot. Or maybe the answer is quite simple, and my lake of experience is playing against me.

Basically, I have a list of articles that can be sorted into categories.

I sorted them this way in the urls:

urlpatterns = patterns( '', url(r'^$', ArticleListView.as_view(), name='articles-list'), url(r'^source/(?P<source>[\w\ .@ +-]+)/$', SourceEntriesView.as_view(), name='articles-source'), url(r'^date/(?P<daterealization>[\w\ .@ +-]+)/$', DateEntriesView.as_view(), name='articles-date'), url(r'^country/(?P<region>[\w\ .@ +-]+)/$', RegionEntriesView.as_view(), name='articles-region'), url(r'^global/$', GlobalEntriesView.as_view(), name='articles-global'), ) 

and main URL mydomain.com/en/press/

So, basically, when I try to sort my articles by source, for example, I have my article in this category. But the page has all the articles.

So, if there is only one article in the category, this article is only shown, but my "loadMore" button is not disabled because it considers that there are more articles.

Here are the main views.py views of the base class, first the base:

 class BaseArticleListView(ListView): """ Base article list view for manage ajax navigation """ model = Article context_object_name = 'article_list' template_name = 'base_templates/template_press.html' paginate_by = get_setting('PAGINATION') def get_load_more_url(self, request, context): args = request.GET.copy() page_obj = context.get('page_obj', None) if not page_obj or not page_obj.has_next(): return '' args[self.page_kwarg] = page_obj.next_page_number() return '?{}'.format(args.urlencode()) def render_to_json_response(self, context, **response_kwargs): if 'current_app' not in context: context['current_app'] = resolve(self.request.path).namespace c = RequestContext(self.request, context) html_items_list = render_to_string( 'base_templates/template_press.html', context, context_instance=c) html_items_list = html_items_list.strip() json_response = { 'html_items_list': html_items_list, 'load_more_url': self.get_load_more_url( self.request, context) } return JsonResponse(json_response) 

And the general view of the list entry:

 #Main article display view class ArticleListView(FormMixin, BaseArticleView, BaseArticleListView, ContextSourcesMixin): model = Article view_url_name = 'djangocms_press:articles-list' form_class = SourcesRegionsFilterForm def get_form_kwargs(self): return { 'initial': self.get_initial(), 'prefix': self.get_prefix(), 'data': self.request.GET or None, 'request': self.request, } def get(self, request, *args, **kwargs): """ Handle the form submissions to filter by Sources and regions First_object is use for pagination """ context = {} self.object_list = self.get_queryset().order_by("-date_realization") first_object = 0 if 'article' in self.request.GET: try: project_id = int(request.GET['article']) context['article_render'] = self.object_list.get(pk=project_id) except (Article.DoesNotExist, ValueError): pass form = self.get_form(self.form_class) if form.is_valid(): if form.cleaned_data['regions']: self.object_list = self.object_list.filter( Q(regions__continent=form.cleaned_data['regions']) | Q(global_regions=True)).distinct() context.update(self.get_context_data(form=form)) context[self.context_object_name] = context['object_list'] source_qs = ArticleSource.objects.active_translations(get_language()).order_by('translations__name') date_realization_for_articles = Article.objects.values_list('date_realization', flat=True).distinct() region_for_articles = Country.objects.exclude(regions_press_article=None).order_by('name') context['load_more_url'] = self.get_load_more_url(request, context) context['dates_realization'] = date_realization_for_articles.dates('date_realization', 'month', order="DESC") context['sources_list'] = source_qs context['regions_list'] = region_for_articles return self.render_to_response(context) def render_to_json_response(self, context, **response_kwargs): if 'current_app' not in context: context['current_app'] = resolve(self.request.path).namespace c = RequestContext(self.request, context) html_items_list = render_to_string( 'base_templates/template_press.html', context, context_instance=c) html_items_list = html_items_list.strip() json_response = { 'html_items_list': html_items_list, 'load_more_url': self.get_load_more_url(self.request, context), } return JsonResponse(json_response) def render_to_response(self, context): if self.request.is_ajax(): response = self.render_to_json_response(context) else: response = super(ArticleListView, self).render_to_response(context) return response 

And you can see that the loadmore button is updated on this line:

context['load_more_url'] = self.get_load_more_url(request, context)

end FINALLY class that controls the sorting of sources:

 class SourceEntriesView(ContextSourcesMixin, BaseArticleView, BaseArticleListView): context_object_name = 'article_list' template_name = 'base_templates/template_press.html' _source = None view_url_name = 'djangocms_press:articles-source' def get(self, *args, **kwargs): # submit object to cms toolbar to get correct language switcher behavior if hasattr(self.request, 'toolbar'): self.request.toolbar.set_object(self.source) return super(SourceEntriesView, self).get(*args, **kwargs) @property def source(self): if not self._source: try: source_qs = ArticleSource.objects.active_translations( get_language(), slug=self.kwargs['source'] ) #source_qs = source_qs.filter(site=Site.objects.get_current().pk) self._source = source_qs.latest('pk') except ArticleSource.DoesNotExist: raise Http404("ArticleSource does not exist for this site") return self._source def get_queryset(self): qs = super(SourceEntriesView, self).get_queryset() if 'source' in self.kwargs: qs = qs.filter(sources__pk=self.source.pk) return qs def get_context_data(self, **kwargs): kwargs['source'] = self.source context = super(SourceEntriesView, self).get_context_data(**kwargs) return context 

So this last class is a call in ajax - from a URL - when choosing a source. But how to update the download button more from here? I am so lost.

After reading the pagination and base class documentation, my conclusion would be to delete this line. But I'm afraid to break everything and that I can’t get it to work properly.

So my question is , what would be the best way to update pagination to sort by some object categories?

Thank you in advance, at least for the time spent on my request.

+7
python django django-views
source share
1 answer

So, since it was quite difficult (but finally not), I rewrote everything.

And now it works, again on a good old day (erase / code again).

Thanks to everyone who took the time to read my request, even if, unfortunately, no one could give an answer!

+3
source share

All Articles