简体   繁体   English

Django_comments:覆盖post_comment()的正确方法是什么?

[英]Django_comments: What's the proper way to override post_comment()?

I'm trying to customize django_comments app ( github repo , docs ). 我正在尝试自定义django_comments应用程序( github repodocs )。

The default get_form_target() returns django_comments.views.post_comment , which is a function-based view. 默认的django_comments.views.post_comment get_form_target()返回django_comments.views.post_comment ,它是基于函数的视图。 post_comment() , in turn, returns a HttpRedirectResponse to the value of next (if specified) or to a default confirmation page. post_comment()依次将HttpRedirectResponse返回到next (如果已指定)的值或默认的确认页面。

See default post_comment() implementation here . 请参阅此处的默认post_comment()实现。

I need to add a couple lines of logic to the post_comment() . 我需要在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. 我可以复制原始代码并对其进行修改,但是感觉很像是重复了几行代码,只是添加了几行自定义逻辑。

It depends on where your logic happens relative to what is happening in the current post_comment() . 这取决于您的逻辑相对于当前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. 如果您只是想 post_comment() 之前做某事,则只需将指针复制到现有功能,创建您的功能,并在最后调用原始功能。 Finally, you can MonkeyPatch it in by assigning your function to the other module's post_comment() . 最后,您可以通过将函数分配给其他模块的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. OTOH,如果您要更改当前func 内部发生的事情,则可能必须先复制代码,然后复制上面的MonkeyPatch。

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. 请注意:即使有很好的评论警告人们这是在做什么,MonkeyPatching还是维护着一个炸弹,只是在将来某个不方便的时间等待启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 努力使django_comments与Django REST Framework一起使用 - Struggling to get django_comments to work with Django REST Framework 由于django_comments,将应用程序推送到Heroku失败 - Pushing app to Heroku failed because of django_comments 没有电子邮件的用户无法使用 Django 的评论框架发表评论 - A user with no email can't post a comment using Django's comments framework Django-实现线程注释的正确方法 - Django - proper way to implement threaded comments 用Django形式覆盖POST数据的正确方法是什么? - What is correct way to override POST data in a Django form? 在 Python 中注释代码的正确方法是什么? - What is the proper way to comment code in Python? 在 Python 中注释函数的正确方法是什么? - What is the proper way to comment functions in Python? 使用Django评论时获得“不允许发表评论(400)” - Getting “Comment post not allowed (400)” when using Django Comments Converting Django project from Python 2 to Python 3: pip3 install django_comments NameError unicode - Converting Django project from Python 2 to Python 3: pip3 install django_comments NameError unicode 用UML绘制Django应用程序文件的正确方法是什么? - What's the proper way to diagram a Django application file in UML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM