1

I'm trying to customize django_comments app (github repo, docs).

The default get_form_target() returns django_comments.views.post_comment, which is a function-based view. post_comment(), in turn, returns a HttpRedirectResponse to the value of next (if specified) or to a default confirmation page.

See default post_comment() implementation here.

I need to add a couple lines of logic to the post_comment(). Can I override it to where it still does the default implementation, but with the extra logic? I could just copy the original code and modify it, but it feels like a lot of code duplication for just adding a couple lines of custom logic.

Vicky Leong
  • 1,208
  • 3
  • 12
  • 30

1 Answers1

1

It depends on where your logic happens relative to what is happening in the current post_comment(). If you simply want to do something before post_comment(), then just copy a pointer to the existing func, create your func, and call the original at the end. Finally, you can MonkeyPatch it in by assigning your function to the other module's post_comment().

OTOH, if what you want to do is change what's happening inside the current func, then you may have to make a copy of the code, then MonkeyPatch as above.

Note well: even with good comments warning people about what this is doing, MonkeyPatching represents a maintenance timebomb just waiting to go off at some inconvenient time in the future.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65