首页 > 解决方案 > 使用 Django 从另一个 CBV 获取上下文

问题描述

我想获得您的帮助,以便通过get_context_data().

客观的:

基本上,我有一个ListView()带有pagination. 我拿起当前页面在get_context_data(). 然后,我想编辑一个对象,所以我要去UpdateView().

当我发布编辑时,它应该返回到以前的分页。例如,如果对象在第 32 页,在 之后POST request,我需要被重定向到第 32 页。

我的代码:

class DocumentListView(AdminMixin, CustomListSearchView):
    """ Render the list of document """

    def get_context_data(self, **kwargs):
        context = super(DocumentListView, self).get_context_data(**kwargs)

        actual_page = self.request.GET.get('page', 1)
        if actual_page:
            context['actual_page'] = actual_page
        return context

class DocumentFormView(CustomPermissionRequiredMixin, DocumentListView, UpdateView):

    def get_current_page(self, **kwargs):
        context = super(DocumentListView, self).get_context_data(**kwargs)
        actual_page = context['actual_page']
        return actual_page

    def get_context_data(self, **kwargs):
        context = super(DocumentFormView, self).get_context_data(**kwargs)
        print(self.get_current_page())
        ...       
        return context

    def post(self, request, pk=None):
        ...
        return render(request, 'app/manage_document_form.html', context)

问题:

我没有克服actual_page在我的班上DocumentFormView

我遇到了这个问题:

AttributeError:“DocumentFormView”对象没有属性“对象”

我试图添加我的post()功能:

self.object = self.get_object()

但这并不能解决问题。

你有什么主意吗 ?

标签: pythondjangodjango-viewsdjango-pagination

解决方案


推荐阅读