0

我有一个父视图和一个子视图。我想在子视图中覆盖一个自定义函数,如下所示:

我的网址:

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

我想覆盖父视图 next_url 并将其传递给 post()

目前输出只打印:“NEXT URL: ...”

我该如何解决这个问题?提前致谢

4

1 回答 1

1

您的意见代码是正确的。

您的edit_images.html模板很可能 POST 到AddImages视图,这使得它看起来好像EditImages视图的覆盖函数没有被调用。同时只是因为触发了错误的视图。

于 2019-02-12T17:42:17.343 回答