在基于类的django视图中获取其他数据

时间:2018-04-16 14:50:33

标签: python django

我跟随Mozilla Django Tutorial并创建了以下视图:

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_context_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

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

2 个答案:

答案 0 :(得分:1)

问题是您使用全新词典替换了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

答案 1 :(得分:0)

当你这样做时:

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