Python 在基于类的django视图中获取附加数据

Python 在基于类的django视图中获取附加数据,python,django,Python,Django,Im跟踪并创建以下视图: view.py class BookListView(generic.ListView): model = Book queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war context_object_name = 'book_list' # your own name for the list as

Im跟踪并创建以下视图:

view.py

class BookListView(generic.ListView):
    model = Book
    queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
    context_object_name = 'book_list'   # your own name for the list as a template variable
    template_name = 'catalog/index.html'  # Specify your own template name/location
    paginate_by = 3
我已重新组织了项目中的文件,并希望显示模板上的其他信息。我尝试使用
get\u context\u data(self,**kwargs):
来实现这一点,但是我只得到了我在这个方法中定义的内容

更改为:

class BookListView(generic.ListView):
    model = Book
    #queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
    #context_object_name = 'book_list'   # your own name for the list as a template variable
    template_name = 'catalog/index.html'  # Specify your own template name/location
    paginate_by = 3

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get the context
        context = super(BookListView, self).get_context_data(**kwargs)

        display_books = Book.objects.filter(title__icontains='war')[:5]

        # Generate counts of some of the main objects
        num_books=Book.objects.all().count()
        num_instances=BookInstance.objects.all().count()

        # Available books (status = 'a')
        num_instances_available=BookInstance.objects.filter(
                                                status__exact='a').count()
        num_authors=Author.objects.count()  # The 'all()' is implied by default.          

        # Create any data and add it to the context
        context = {
           'display_books':display_books,
           'num_books':num_books,
           'num_instances':num_instances,
           'num_instances_available':num_instances_available,
           'num_authors':num_authors
        }
        return context

这种方式是我的分页不起作用。

问题是您用一个全新的字典替换了
super()
调用中的
上下文

def get_context_data(self, **kwargs):

    context = super(BookListView, self).get_context_data(**kwargs)
    ...
    context = {
       'display_books':display_books,
       'num_books':num_books,
       'num_instances':num_instances,
       'num_instances_available':num_instances_available,
       'num_authors':num_authors
    }
    return context
您应该更新现有词典:

def get_context_data(self, **kwargs):
    context = super(BookListView, self).get_context_data(**kwargs)
    ...
    context['display_books'] = display_books
    context['num_books'] = num_books
    context['num_instances'] = num_instances
    context['num_instances_available'] = num_instances_available
    context['num_authors'] = num_authors
    return context
当您这样做时:

context = super(BookListView, self).get_context_data(**kwargs)
您获得了上下文,但在最后重新分配了上下文:

context = {
       'display_books':display_books,
       'num_books':num_books,
       'num_instances':num_instances,
       'num_instances_available':num_instances_available,
       'num_authors':num_authors
    }
您需要做的是将元素添加到dict中:

context["display_books"] = display_books
context["num_books"] = num_books
context["num_instances"] = num_instances
context["num_instances_available"] = num_instances_available
context["num_authors"] = num_authors