django comments: how to prevent form errors from redirecting the user to the preview page?
-
19-09-2019 - |
Question
Currently, django.contrib.comments sends the user to the preview page if there is any error on the form.
I am using comments in the context of a blog and I would much rather that the user stayed on the page they were on if something went wrong with the submission. As far as I can tell though, this is hard-coded in django.contrib.comments.views.comments.post_comment:
# If there are errors or if we requested a preview show the comment
if form.errors or preview:
template_list = [
"comments/%s_%s_preview.html" % tuple(str(model._meta).split(".")),
"comments/%s_preview.html" % model._meta.app_label,
"comments/preview.html",
]
return render_to_response(
template_list, {
"comment" : form.data.get("comment", ""),
"form" : form,
"next": next,
},
RequestContext(request, {})
)
Is there any way that I can change this behavior without changing the source code to django.contrib.comments?
Any pointer would be appreciated...
Thanks!
Solution
Looks like you have two real options:
- Write your own view. Possibly copy that view's code to get started.
- Patch that view to take an extra parameter, such as 'preview_on_errors' which defaults to True but can be overridden. Contribute the patch back to Django so other people can benefit from it.
OTHER TIPS
Yes! There's now a way to customize the Comments app. Good luck!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow