0

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 ?

Essex
  • 6,042
  • 11
  • 67
  • 139
  • This isn't at all what you want to do. It doesn't make any sense to use `super` like that, let alone have two separate methods calling two separate super methods. But quite apart from anything else, the data is coming from the request; if you need the value from the `page` parameter in the form view, it needs to be present in the request for that view. – Daniel Roseman Mar 08 '19 at 09:51
  • @DanielRoseman Ok, but if I understand well the url according to my form view doesn't display any information from pagination in `ListView()`. So I don't see how I can pick up data from url through a `GET` request. In my `ListView()`, the url looks like this : `http://localhost:8000/crud/document/list/?page=72` and in my `UpdateView()` : `http://localhost:8000/crud/document/update/150/` – Essex Mar 08 '19 at 09:59
  • If it's not in the URL there is **no way** your view can get it. Your code is doubly mistaken in that case. You need to actually include the parameter in the URL you use for the form view. – Daniel Roseman Mar 08 '19 at 10:05
  • Hum ok I have to see how I can do that. Thank you for your explanation anyway ! – Essex Mar 08 '19 at 10:08

0 Answers0