I would like to get your help in order to use context from a Django CBV in another CBV through get_context_data()
.
Objective:
Basically, I have a django ListView()
with pagination
. I pick up the current page in get_context_data()
. Then, I would like to edit an object, so I'm going to UpdateView()
.
When I post the edit, it should return to the previous pagination. For example, if the object was on page 32, after the POST request
, I need to be redirected to page 32.
My code:
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)
Issue:
I don't overcome to get the actual_page
in my class DocumentFormView
.
I got this issue :
AttributeError: 'DocumentFormView' object has no attribute 'object'
I tried to add in my post()
function :
self.object = self.get_object()
But it doesn't solve the issue.
Do you have any idea ?