我想用django做一些簡單容易的事情:Django分頁返回所有元素,但仍然分頁
有一個項目列表的分頁。
所以,我有一篇文章頁面,我想最多顯示,每頁10條,翻頁= 10個一篇文章等
您可以通過一些類別,來源或日期排序。
所以,當我到達我的mydomaine.com/articles/我顯示了我所有的元素。
如果我排序的來源,因爲這views.py顯示:
class SourceEntriesView(ContextSourcesMixin, BaseArticleView, BaseArticleListView):
model = Article
context_object_name = 'article_list'
template_name = 'base_templates/template_press.html'
_source = None
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_press:articles-source'
@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
return super(SourceEntriesView, self).get_context_data(**kwargs)
一旦Ajax調用是由加載此視圖,並顯示按來源分類的文章,我有我的10個項目! (好吧,我正在測試時使用4)。
那麼爲什麼它不在主頁上工作。
這裏有更多的解釋我的代碼:
''' Main article display view '''
class ArticleListView(FormMixin, BaseArticleListView, ContextSourcesMixin):
model = Article
context_object_name = 'article_list'
template_name = 'base_templates/template_press.html'
view_url_name = 'djangocms_press:articles-list'
form_class = SourcesRegionsFilterForm
paginate_by = get_setting('PAGINATION')
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['load_more_url'] = self.get_load_more_url(request, context)
context[self.context_object_name] = self.object_list
context['object_list'] = self.object_list
source_qs = ArticleSource.objects.active_translations(get_language())
context['sources_list'] = source_qs
date_realization_for_articles = Article.objects.values_list('date_realization',
flat=True).distinct()
context['dates_realization'] = date_realization_for_articles.dates('date_realization', 'month', order="DESC")
return self.render_to_response(context)
def get_load_more_url(self, request, context):
args = request.GET.copy()
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)
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
這是顯示文章的主要觀點。
我的設置,非常簡單但更容易使用:
def get_setting(name):
from django.conf import settings
from meta_mixin import settings as meta_settings
default = {
'PRESS_PAGINATION': getattr(settings, 'PRESS_PAGINATION', 4),
}
return default['PRESS_%s' % name]
我也試圖通過在文章列表的URL paginated_by但結果是一樣的。
這裏是我的模板的一部分猜想進行分頁:
{% if article_list %}
<div id="articles-list" class="col-md-12">
{% for article in article_list %}
<div class="row article">
<div class="col-md-2">
{{ article.date_realization|date:"F d, Y" }}
</div>
<div class="col-md-2">
{{ article.source }}
</div>
<div class="col-md-2">
{% for region in article.regions.all %}
{{ region.name }}
{% endfor %}
</div>
<div class="col-md-6">
{{ article.title }}
</div>
</div>
{% endfor %}
</div>
{% if is_paginated %}
<section id="content-btn">
<div id="load-more-btn blippar-white-btn" class="col-md-2 col-md-offset-5 col-xs-8 col-xs-offset-2" align="center">
<a id="loadmore" role="button" class="button btn load-more-btn blippar-white-btn" href="{{ load_more_url }}">{% trans 'VIEW MORE' %}</a>
</div>
</section>
<div class="col-lg-12">
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</div>
{% endif %}
{% endif %}
所以,我有2個問題:
- 爲什麼沒有顯示主力文章列表上?
- 爲什麼選擇來源後,如果點擊「下一步」,我的所有項目都會顯示?
如果你有一個想法或反射的方向,我會喜歡它:)
這個問題沒有明確描述:當你去到錯誤的觀點(ArticleListView)你沒有任何結果?然後當你選擇一個源時,你突然間所有的結果沒有分頁? – Mijamo
在錯誤的觀點,我有我的文章顯示,沒有分頁,當我選擇一個來源,它得到分頁。 –
好吧我現在明白了,我想我找到了問題,所以我提出了一個解決方案。 – Mijamo