Reputation: 2727
I want to pass a 'success' message to the post, after a new comment is added. Here are the views:
@login_required
def add_comment(request, post_slug):
p= Article.objects.get(slug=post_slug)
cform = CommentForm(request.POST)
if cform.is_valid():
c = cform.save(commit = False)
c.post = p
c.body = cform.cleaned_data['body']
c.author = request.user
c.save()
messages.success(request, "Comment was added")
else:
messages.error(request, "Comment Invalid!")
return redirect('article.views.post_withslug', post_slug=post_slug)
and
def post_withslug(request, post_slug):
post = Article.objects.get(slug = post_slug)
comments = Comment.objects.filter(post=post)
d = dict(post=post, comments=comments, form=CommentForm(),
user=request.user)
d.update(csrf(request))
return render_to_response("article/post.html", d)
The problem is that, messages are not displayed immediately after redirecting to post with new comment. They are displayed however, when I navigate to other pages.
I guess this is because the context_instance = RequestContext(request)
is not properly conveyed from comment to post views. So I tried other redirect methods like render_to_respons
and redner(reverse(
, but could
not manage to solve the issue. So appreciate your hints.
Upvotes: 0
Views: 2929
Reputation: 599630
The problem is in the post_withslug
view. You are not actually using a RequestContext in that view. d
is a standard dictionary. So, context processors will not be run and the messages will not appear.
You should use the render
shortcut instead of render_to_response
:
return render(request, "article/post.html", d)
Upvotes: 1