pythondjangodjango-viewsdjango-class-based-viewsdjango-inheritance

Custom Function in Django class based view not being overriden


I have a parent View, and a child View. I want to override one custom function in the child view like so:

My URLs:

path('<int:place_id>/add_images/', AddImages.as_view(), name="add-images"),
path('<int:place_id>/edit/images/', EditImages.as_view(), name="edit-images"),
# PARENT:
class AddImages(LoginRequiredMixin, View):
    template_name = images/add_images.html

    # HERE I GET THE CURRENT URL:
    def next_url(self):
        next_url = "?next={0}".format(self.request.path)
        print("NEXT URL:" + str(next_url))
        return next_url

    # AND I USE IT HERE:
    def post(self, request, **kwargs):
        # ...
        next_url = self.next_url()
        data = {'next_url' : next_url }
        return JsonResponse(data)


# CHILD:
class EditImages(AddImages):
    """
    Inherits from ImagesView. overwrites template and next_url

    """
    template_name = "images/edit_images.html"

    def next_url(self):
        next_url = "?next={0}".format(self.request.path)
        print("CHILD URL2:" + str(next_url))
        return next_url

I want to override the parent views next_url and pass it on to post()

currently the output only prints: "NEXT URL: ..."

How can I solve this? Thanks in advance


Solution

  • Your views code is correct.

    Your edit_images.html template most likely POSTs to the AddImages view, which makes it appear as if the EditImages view's overridden function is not called. Meanwhile it is just because the wrong view is triggered.