Question

I want to create a simple message of "Your comment has been posted!" using Django's messages framework. I am using Django's comments framework and set it up so that after posting a comment it refreshes the page with

<div><input type="hidden" name="next" value="{{ request.get_full_path }}" /></div>

inside my form.html that I created to override the comments's default. I also had to include django.core.context_processors.request for TEMPLATE_CONTEXT_PROCESSORS inside my settings.py.

Anyway, the messages documentation says to add messages inside views.py. Does that means I need to override the comments's views.py or is there a simplier way to do this? I am a bit uncomfortable modifying the source code for comments. If I had to, I'm not even sure where to add the line

 messages.add_message( request, messages.SUCCESS, 'You comment has been posted!' )

under def post_comment() in django.contrib.comments.views.comments.

Was it helpful?

Solution

You can connect a custom receiver to comment_was_posted signal. It can look like this:

from django.contrib.comments.signals import comment_was_posted

def thank_user(sender, comment=None, request=None, **kwargs):
    messages.add_message( request, messages.SUCCESS, 'You comment has been posted!' )
comment_was_posted.connect(thank_user)

A nice place for such a snippet is a project_specific models.py, because they are all imported at model definition time.

OTHER TIPS

The secret is that when posting a comment the comments app sends a signal. So set up a receiver to handle the comment_was_posted (or comment_will_by_posted) signal. In the receiver call messages.add_message(...)

The docs for this are: https://docs.djangoproject.com/en/1.4/ref/contrib/comments/signals/#module-django.contrib.comments.signals

and

https://docs.djangoproject.com/en/1.4/topics/signals/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top