問題は少し複雑です。 実際、私はホイールを再発明しようとしていないし、バックエンドの開発者が残して以来、私は自分のコードを破壊しないように最善を尽くしています。Django views.pyカテゴリベースビューのカテゴリ選択からページネーションを更新する
しかし、私は今回は多くのことを変える必要があると思います。あるいは、答えがとてもシンプルで、経験の湖が私に対抗するかもしれません。
基本的にはカテゴリ別に並べ替えることができる記事のリストがあります。
私はURLでそれらをこのように並べ替え:
urlpatterns = patterns(
'',
url(r'^$', ArticleListView.as_view(), name='articles-list'),
url(r'^source/(?P<source>[\w\[email protected]+-]+)/$', SourceEntriesView.as_view(), name='articles-source'),
url(r'^date/(?P<daterealization>[\w\[email protected]+-]+)/$', DateEntriesView.as_view(), name='articles-date'),
url(r'^country/(?P<region>[\w\[email protected]+-]+)/$', RegionEntriesView.as_view(), name='articles-region'),
url(r'^global/$', GlobalEntriesView.as_view(), name='articles-global'),
)
とメインURLは私がソースによって私の記事を並べ替えるしようとすると、例えば、私は私の記事を持っているので、基本的にmydomain.com/en/press/
ですこのカテゴリ表示でしかし、ページネーションにはまだすべての記事があります。
カテゴリ内に記事が1つしかない場合は、この記事のみ表示されますが、「loadMoreボタン」は無効になっていません。
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)
と汎用リスト・エントリ・ビュー:
#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
そして、あなたはloadmoreボタンがあることがわかります。ここ
は、第1基地、プリンシパルviews.pyベースクラス図であります
context['load_more_url'] = self.get_load_more_url(request, context)
エンドFI:このラインでの更新ナリーソートされたソースを管理クラスは:
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
ので、この最後のクラスは、Ajaxで呼び出しがある - URLから - あなたがソースを選択したとき。しかし、より多くのボタンをここから更新するにはどうすればいいですか?私はとても迷っています。
ページネーションとベースクラスのビューについてのドキュメントを読んだ後で、私の結論はそれらのラインを削除することです。しかし、私はすべてを壊すことを恐れ、私はそれが適切に動作するようにはできません。
私の質問はです。いくつかのカテゴリでオブジェクトを並べ替えるためのページ設定を更新する最も良い方法は何でしょうか?
少なくとも私の要求に費やしてくれてありがとう、ありがとうございます。