[Django]-Django Comment, append symbol to the url comment?


I just stumbled across this little bit of ugliness. After reading the source code I didn’t see any nice way to override this behavior. By default you are redirected to the URL in the template’s {{ next }} variable and Django appends a ?c=1 to the URL where the 1 is the ID of the comment. I wanted this to instead be #c1 so the user is jumped down the page to the comment they just posted. I did this with a little bit of “monkey patching” as follows:

from django.contrib.comments.views import utils
from django.core import urlresolvers
from django.http import HttpResponseRedirect

def next_redirect(data, default, default_view, **get_kwargs):
    next = data.get("next", default)
    if next is None:
        next = urlresolvers.reverse(default_view)
    if get_kwargs:
        next += '#c%d' % (get_kwargs['c'],)
    return HttpResponseRedirect(next)

# Monkey patch
utils.next_redirect = next_redirect

Leave a comment