Ticket #11549: comments_patch.diff

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

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

  • django/contrib/comments/views/comments.py

     
    2222        if settings.DEBUG:
    2323            self.content = render_to_string("comments/400-debug.html", {"why": why})
    2424
    25 def post_comment(request, next=None):
     25def post_comment(request, next=None, error_page=None):
    2626    """
    2727    Post a comment.
    2828
    2929    HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
    3030    errors a preview template, ``comments/preview.html``, will be rendered.
     31   
     32    If error_page is given, error_page['template'] will
     33    be used as the template instead of the preview page, and the key/value
     34    pairs of error_page['data'] will be used as appended to the context.
    3135    """
    3236    # Fill out some initial data fields from an authenticated user, if present
    3337    data = request.POST.copy()
     
    7983            "comments/%s_preview.html" % model._meta.app_label,
    8084            "comments/preview.html",
    8185        ]
    82         return render_to_response(
    83             template_list, {
     86        data_dict = {
    8487                "comment" : form.data.get("comment", ""),
    8588                "form" : form,
    8689                "next": next,
    87             },
     90            }
     91        if form.errors and error_page:
     92            # If the user has passed a custom error page, take its template
     93            # and data dict and use those instead of the preview page
     94            if error_page.has_key('template'):
     95                template_list = [error_page['template']] + template_list
     96            try:
     97                for (key, value) in error_page['data'].items():
     98                    data_dict[key] = value
     99            except TypeError: pass
     100        return render_to_response(
     101            template_list,
     102            data_dict,
    88103            RequestContext(request, {})
    89104        )
    90105
  • docs/ref/contrib/comments/custom.txt

     
    118118
    119119.. _custom-comment-app-api:
    120120
     121Customizing which template renders the errors
     122=============================================
     123
     124The ``post_comment`` view in ``contrib/comments/views/comments.py`` can take an
     125extra "error_page" argument which conatins a dict contains another template
     126location and extra context data::
     127
     128    error_page = {
     129        'template': 'your_template_directory/template_name.html',
     130        'data': {
     131            'key1': value1,
     132            'key2: vaue 2,
     133            },
     134    }
     135
     136
     137This will make django load the specified template instead of the default
     138preview one. This allows you to keep the user on the same page if they forget
     139one of the comments fields by defining a wrapper around the ``post_comment``
     140view which you point ``comments/post`` url to.
     141
    121142Custom comment app API
    122143======================
    123144
Back to Top