如何在一个视图中分别对多个对象进行分页?

时间:2019-07-04 06:36:32

标签: django

我有3个物体,潜艇和潜艇。在任何时间点都会显示一个,其他两个会被隐藏。我该如何分别对这3个页面进行分页?

我看到类似的问题,连锁店解决了这个问题。但是,在我的情况下,我不想将这3个合并在一起,因此我可以基于一个按钮选择要在html中显示的那个。

我基于班级的视图

class SubListView(ListView):
model = Post
template_name = 'store/sub_home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 12

def get(self, request):
    if request.user.is_authenticated:
        paginate_by = 12
        post = Post.objects.all().order_by('-date_posted')
        users = User.objects.exclude(id=request.user.id)
        sub = Subscriber.objects.get(current_user=request.user)
        subs = sub.users.all()
        my_content = Post.objects.filter(author=request.user.id)
        args={
            'posts':post, 'users':users, 'subs':subs, 'mine':my_content
        }
        return render(request, self.template_name, args)
    else:
        post = Post.objects.all().order_by('-date_posted')
        paginate_by = 12
        args={
            'posts':post, 
        }           
        return render(request, self.template_name, args)

这是我的html:

{% extends "store/base.html" %}
{% block content %}
<div class="panel">
      <!-- <a href="{% url 'store-home'%}"> -->
        <button class="Active" id="random_link" onclick="show('random'); return true;">
        <img src="/static/store/random.gif" alt="">
        Random Finds
        </button>
      {% if user.is_authenticated %}

      <button id="subscription_link" onclick="show('subscription'); return true;">
      {% else %}
      <a href="{% url 'login' %}">
      {% endif %}
        <img src="/static/store/Subscription.gif" alt="">
        My Subscription
      </button>

      {% if user.is_authenticated %}
      <!-- <a  href="{% url 'user-posts' user.username %}"> -->
      <button id="content_link" onclick="show('content'); return true;">
      {% else %}
      <a href="{% url 'login' %}">
      {% endif %}
        <img src="/static/store/content.gif" alt="">
        My Content
      </button>
  </div>
  <p style="padding: 20px;"></p>
  <div style="padding-top: 20px;" id="main" >
<section class="text-center mb-4">
  <div class="row" id="random"> 
    {% for post in posts %}   
    {% include "store/card.html" %}
    {% endfor %}
  </div>
  <div class="row" id="subscription" style="display: none;">
    {% if not subs %}
    <h2>You have not subscribed to any course :/</h2>
    {% endif %} 
    {% for post in subs %}
    {% include "store/card.html" %}
    {% endfor %}
  </div>
  <div class="row" id="content" style="display: none;"> 
    {% if not mine %}
    <h2>You have not published anything :/</h2>
    {% endif %}   
    {% for post in mine %}
    {% include "store/card.html" %}
    {% endfor %}
  </div>
</section>
{% include "store/pagination.html" %}
</div>
{% endblock content %}

这是我的脚本:

function show(z) {
var x = document.getElementById("random");
x.style.display = "none";
var x = document.getElementById("subscription");
x.style.display = "none";
var x = document.getElementById("content");
x.style.display = "none";
var x = document.getElementById(z);
x.style.display = "block";
var element = document.getElementById("random_link");
element.classList.remove("Active");
var element = document.getElementById("subscription_link");
element.classList.remove("Active");
var element = document.getElementById("content_link");
element.classList.remove("Active");
var element = document.getElementById(z+"_link");
element.classList.add("Active");
}

0 个答案:

没有答案