Code

Ticket #11549: comments_patch.diff

File comments_patch.diff, 3.0 KB (added by teebes, 5 years ago)

Patch to contrib.comments (code & documentation) to allow error template selection

Line 
1Index: django/contrib/comments/views/comments.py
2===================================================================
3--- django/contrib/comments/views/comments.py   (revision 11325)
4+++ django/contrib/comments/views/comments.py   (working copy)
5@@ -22,12 +22,16 @@
6         if settings.DEBUG:
7             self.content = render_to_string("comments/400-debug.html", {"why": why})
8 
9-def post_comment(request, next=None):
10+def post_comment(request, next=None, error_page=None):
11     """
12     Post a comment.
13 
14     HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
15     errors a preview template, ``comments/preview.html``, will be rendered.
16+   
17+    If error_page is given, error_page['template'] will
18+    be used as the template instead of the preview page, and the key/value
19+    pairs of error_page['data'] will be used as appended to the context.
20     """
21     # Fill out some initial data fields from an authenticated user, if present
22     data = request.POST.copy()
23@@ -79,12 +83,23 @@
24             "comments/%s_preview.html" % model._meta.app_label,
25             "comments/preview.html",
26         ]
27-        return render_to_response(
28-            template_list, {
29+        data_dict = {
30                 "comment" : form.data.get("comment", ""),
31                 "form" : form,
32                 "next": next,
33-            },
34+            }
35+        if form.errors and error_page:
36+            # If the user has passed a custom error page, take its template
37+            # and data dict and use those instead of the preview page
38+            if error_page.has_key('template'):
39+                template_list = [error_page['template']] + template_list
40+            try:
41+                for (key, value) in error_page['data'].items():
42+                    data_dict[key] = value
43+            except TypeError: pass
44+        return render_to_response(
45+            template_list,
46+            data_dict,
47             RequestContext(request, {})
48         )
49 
50Index: docs/ref/contrib/comments/custom.txt
51===================================================================
52--- docs/ref/contrib/comments/custom.txt        (revision 11325)
53+++ docs/ref/contrib/comments/custom.txt        (working copy)
54@@ -118,6 +118,27 @@
55 
56 .. _custom-comment-app-api:
57 
58+Customizing which template renders the errors
59+=============================================
60+
61+The ``post_comment`` view in ``contrib/comments/views/comments.py`` can take an
62+extra "error_page" argument which conatins a dict contains another template
63+location and extra context data::
64+
65+    error_page = {
66+        'template': 'your_template_directory/template_name.html',
67+        'data': {
68+            'key1': value1,
69+            'key2: vaue 2,
70+            },
71+    }
72+
73+
74+This will make django load the specified template instead of the default
75+preview one. This allows you to keep the user on the same page if they forget
76+one of the comments fields by defining a wrapper around the ``post_comment``
77+view which you point ``comments/post`` url to.
78+
79 Custom comment app API
80 ======================
81