从视图中的所有页面获取内容

时间:2018-06-02 13:29:54

标签: python django pagination

我正在尝试使用Django创建一个博客。该博客提供了我使用javascript制作的存档脚本。这个脚本直接从我的模板中调用,带有一个参数,其中包含所有文章的列表。为了允许分页,我使用内置于分页插件的djangos。

自从实现分页以来,我遇到了一个问题。现在,对我的归档脚本的调用中使用的变量只包含当前页面中的文章,因此归档文件只包含当前页面中的文章。有没有什么方法可以直接从模板中获取每个页面的所有内容,并使用它来调用我的存档功能?

views.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator

from .models import Article

class IndexView(generic.ListView):
    template_name = 'blog/index.html'
    context_object_name = 'latest_article_list'
    paginate_by = 1

    def get_queryset(self):
        """Return the published articles. Newest first."""
        return Article.objects.order_by('-pub_date')

class DetailView(generic.DetailView):
    model = Article
    template_name = 'blog/detail.html'

的index.html

{% include "blog/header.html" %}
{% load jsonify %}

<div class="container">
    <div id="header">
        <ul>
            <li><a class="selected" href="/">blog</a></li>
            <li><a href="/projects/">projects</a></li>
            <li><a href="/contact/">contact</a></li>
        </ul>
    </div><hr>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-9 content-main">
            {% if article_list %}
                {% for article in article_list %}
                    <div class="article">
                        <h1><a href="{% url 'blog:detail' article.id %}">{{ article.title }}</a></h1>
                        <span class="badge badge-default {{ article.subject }}">{{ article.subject }}</span>
                        <i>{{ article.pub_date }}</i>
                        <p>{{ article.body |safe |linebreaks }}</p>
                    </div><br><hr>
                {% endfor %}
            {% else %}
                <p>No articles are available.</p>
            {% endif %}

            {% if is_paginated %}
                <div class="pages">
                    <ul class="pagination">
                        {% if page_obj.has_previous %}
                            <li><a href="?page={{ page_obj.previous_page_number }}"></a></li>
                        {% else %}
                            <li class="disabled"><span></span></li>
                        {% endif %}
                        {% for i in paginator.page_range %}
                            {% if page_obj.number == i %}
                                <li class="active"><span><button type="button" class="btn btn-dark btn-sm">{{ i }}</button> <span class="sr-only">(current)</span></span></li>
                            {% else %}
                                <li><a href="?page={{ i }}"><button type="button" class="btn btn-secondary btn-sm available">{{ i }}</button></a></li>
                            {% endif %}
                        {% endfor %}
                        {% if page_obj.has_next %}
                            <li><a href="?page={{ page_obj.next_page_number }}"></a></li>
                        {% else %}
                            <li class="disabled"><span></span></li>
                        {% endif %}
                    </ul>
                </div>
            {% endif %}
        </div>

        <div class="col-sm-3 content-sidebar">    
            <h2>Archive</h2>
            <div id="archive">
                <script type="text/javascript">

                    // -- ATTENTION --
                    // the argument article_list should contain ALL articles in database
                    // Pagination causes this list to only contain the articles from the current page
                    // -- ATTENTION --

                    var articles = {{ article_list|jsonify }};
                    document.getElementById("archive").innerHTML = GetArchive(articles);
                </script>
            </div>
        </div>
    </div>
</div>

{% include "blog/footer.html" %}

这个问题并不重要,但这是我的档案脚本。

// - Archive generation -
// Loop through each article
// Add each undiscovered year, month and article 
// to a list and wrap each year and month in
// a details tag.

function GetArchive(articles) {
    var monthNames = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"];

    var archive = {}

    articles.forEach(function(article){
        var date = new Date(article.fields.pub_date);
        var year = date.getFullYear();
        var month = date.getMonth();

        if (!archive.hasOwnProperty(year)) archive[year] = {}
        if (!archive[year].hasOwnProperty(month)) archive[year][month] = {}

        archive[year][month][article.fields.title + "<span style='position:absolute; margin-top:3px; margin-left:5px;' class='badge badge-default "+ article.fields.subject +"'>"+ article.fields.subject +"</span>"] = article.pk // add the article in it's corresponding year and month'
    });

    var html = [];
    for (var year in archive) { // write each year
        var tmp = "<details open class='archive_year archive'><summary>"+year+"</summary>"
        for (var month in archive[year]) { // write each month in that year
            tmp += "<details open class='archive_month archive'><summary>"+monthNames[month]+"</summary>"
            for (var article in archive[year][month]) { // write each article in that month
                tmp += "<a class='archive_article archive' href='/"+archive[year][month][article]+"'>"+article+"</a><br>"
            }
            tmp += "</details>"
        }
        tmp += "</details>"
        html.push(tmp)
    }

    return html.reverse().join("");
}

0 个答案:

没有答案