Search code examples
djangodjango-urlsdjango-comments

Django Comment, append symbol to the url comment?


im using the comment system, now, i would like to re-write the segment form the url comment and append a symbol #, i want to move the page seccion to the comment list exactly to the last comment user with <a name=#{{comment.id}}?> username </a>

Im using next for redirect the usen when the comment was posted:

{% get_comment_form for object as form %}
<form action="{% comment_form_target %}" method="POST">
  {{ form }}
  <input type="hidden" name="next" value="{{ object.get_absolute_url }}" />
  <input type="submit" name="preview" class="submit-post" value="Preview"></td>  
</form>

But in the Django Doc dont say nothing about rewrite or customizer the comment redirect / url

Any idea?

Thanks


Solution

  • 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