有什么方法可以将过滤后的查询集传递到Django';什么是分页?

有什么方法可以将过滤后的查询集传递到Django';什么是分页?,django,django-views,django-forms,django-queryset,django-pagination,Django,Django Views,Django Forms,Django Queryset,Django Pagination,view.py quick_home.html def quiz(request): question_topic = request.POST.getlist("topic_checkbox") # Retrieves list of topics selected by user question_list = Quiz.objects.filter(Topic_name__in = question_topic) #filters out questi

view.py

quick_home.html

def quiz(request):

    question_topic = request.POST.getlist("topic_checkbox") # Retrieves list of topics selected by user
    question_list = Quiz.objects.filter(Topic_name__in = question_topic) #filters out questions by topics

    paginator = Paginator(question_list,1) # when i pass all the objects rather than the filtered query set it seems to work but when i paginate with the filtered queryset only the first page loads
    page = request.GET.get('page')
    try:
        question_list = paginator.page(page)
    except PageNotAnInteger:
        question_list = paginator.page(1)
    except EmptyPage:
        question_list = paginator.page(paginator.num_pages)

    return render(request,"Quiz/quiz_home.html",{"question_list":question_list})

{%block content%}
{%forQ in question_list%}#循环通过筛选的查询集
{%if question_list.has_next%}
问题{{q.id}
{%csrf_令牌%}
#该表单应使我能够收集用户输入,同时进入for循环中的下一个问题。但当问题结束时,下一页是空白的,没有显示任何内容
{%if q.picture%}

{%endif%} {{q.id}。){{q.question}}
{{q.option1}}
{{q.option2}}
{{q.option3}}

#单击后,它应分页到下一页 {%else%}
{%csrf_令牌%} {%endif%} {%endfor%} {%endblock%}

从本质上说,我试图根据用户选择的主题过滤掉问题。然后对这组问题进行分页,以便每页显示一个问题。

我将向您展示一个分页实例:

views.py

{% block content %}
        {% for q in question_list %} # loops through the filtered queryset
            {% if question_list.has_next %}
                <h3>Question {{q.id}}</h3>
                <form method="POST" action="?page={{ question_list.next_page_number }}">{% csrf_token %}
# The form should enable me to gather the users input whilst simultaneoulsy going to the next question in the for loop. But when question sumbitted the next page is blank showing no contents
                    {% if q.picture %}
                        <img src="/media/{{q.picture}}"> <br>
                    {% endif %}
                    {{q.id}}.) </label><label id="question_text">{{q.question}}</label><br>
                    <input type="hidden" id= "q_id" name="q_id" value="{{q.id}}">
                    <input type="hidden" id= "topic" name="topic" value="{{q.topic}}">
                    <input type="radio" id="opt1" name="options" value="{{q.option1}}" required>{{ q.option1 }}<br>
                    <input type="radio" id="opt2" name="options" value="{{q.option2}}" required>{{ q.option2 }}<br>
                    <input type="radio" id="opt3" name="options" value="{{q.option3}}" required>{{ q.option3 }}<br>
                    <hr>
                    <input type="submit" id="mybtn" value="Submit"> #once clicked it should paginate to next page
                </form>
            {% else %}
                <hr>
                <form action="/home/">{% csrf_token %}
                    <input type="submit" name="End" value="End">
                </form>
            {% endif %}
        {% endfor %}
    {% endblock %}

category/category_detail.html

class CategoryDetail(ListView):  
    model = Task  
    template_name = 'category/category_detail.html'  
    context_object_name = 'task'  
    paginate_by = 5

    def get_queryset(self):
        self.category = get_object_or_404(Category,
                                          pk=self.kwargs['pk'])  
        return Task.objects.filter(category=self.category).order_by('-id')  

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(CategoryDetail, self).get_context_data(**kwargs)
        self.category = get_object_or_404(Category, pk=self.kwargs['pk'])
        # context['category'] = self.category
        return context
。。。。
{%if已分页%}
    {%如果页面_obj.has_previous%}
  • {%else%}
  • {%endif%} {paginator.page_range%} {%如果页码=i%}
  • {%else%}
  • {%endif%} {%endfor%} {%如果页面_obj.has_next%}
  • {%else%}
  • {%endif%}
{%endif%}

Django已经有了现成的分页结构。您可以在您想要使用的Html文件中使用它。

ListView在我们有表单时不起作用,对吗?我正在尝试对过滤后的查询集question\u list=quick.objects.filter(Topic\u name\u in=question\u Topic)进行分页,同时也发布每个页面的数据。如能使用基于函数的视图,将不胜感激。
....

 <div class="card-footer py-4">
                {% if is_paginated %}
              <nav aria-label="...">
                <ul class="pagination justify-content-end mb-0">
                    {% if page_obj.has_previous %}
                  <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}" tabindex="-1">
                      <i class="fas fa-angle-left"></i>
                      <span class="sr-only">Previous</span>
                    </a>
                  </li>

                   {% else %}

                     <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1">
                      <i class="fas fa-angle-left"></i>
                      <span class="sr-only">Previous</span>
                    </a>
                  </li>

                    {% endif %}

                    {% for i in paginator.page_range %}
                    {% if page_obj.number == i %}
                  <li class="page-item active">
                    <a class="page-link" href="#"> {{ i }} </a>
                  </li>
                     {% else %}
                  <li class="page-item">
                    <a class="page-link" href="?page={{ i }}">{{ i }}<span class="sr-only">(current)</span></a>
                  </li>
                    {% endif %}
                    {% endfor %}

                     {% if page_obj.has_next %}
                  <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                      <i class="fas fa-angle-right"></i>
                      <span class="sr-only">Next</span>
                    </a>
                  </li>

                    {% else %}

                     <li class="page-item disabled">
                    <a class="page-link" href="#">
                      <i class="fas fa-angle-right"></i>
                      <span class="sr-only">Next</span>
                    </a>
                  </li>
                    {% endif %}

                </ul>
              </nav>
                {% endif %}
            </div>