Ticket #11100: r10753_get_comment_permalink.diff
File r10753_get_comment_permalink.diff, 4.0 KB (added by , 16 years ago) |
---|
-
django/contrib/comments/templatetags/comments.py
diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py index 9f8180a..4e84ee2 100644
a b def comment_form_target(): 252 252 """ 253 253 return comments.get_form_target() 254 254 255 #@register.simple_tag 256 def get_comment_permalink(comment, anchor_pattern=None): 257 """ 258 Get the permalink for a comment, optionally specifying the format of the 259 named anchor to be appended to the end of the URL. 260 261 Example:: 262 {{ get_comment_permalink comment "#c%(id)s-by-%(user_name)s" }} 263 """ 264 265 if anchor_pattern: 266 return comment.get_absolute_url(anchor_pattern) 267 return comment.get_absolute_url() 268 255 269 register.tag(get_comment_count) 256 270 register.tag(get_comment_list) 257 271 register.tag(get_comment_form) 258 272 register.tag(render_comment_form) 259 273 register.simple_tag(comment_form_target) 274 register.simple_tag(get_comment_permalink) 275 No newline at end of file -
docs/ref/contrib/comments/index.txt
diff --git a/docs/ref/contrib/comments/index.txt b/docs/ref/contrib/comments/index.txt index f6e1553..8d0e943 100644
a b This returns a list of :class:`~django.contrib.comments.models.Comment` objects; 104 104 see :ref:`the comment model documentation <ref-contrib-comments-models>` for 105 105 details. 106 106 107 .. templatetag:: get_comment_permalink 108 109 Linking to comments 110 ------------------- 111 112 To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`:: 113 114 {% get_comment_permalink comment_obj [format_string] %} 115 116 By default, the named anchor that will be appended to the URL will be the letter 117 'c' followed by the comment id, for example 'c82'. You may specify a custom 118 format string if you wish to override this behavior:: 119 120 {% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%} 121 122 The format string is a standard python format string. Valid mapping keys 123 include any attributes of the comment object. 124 125 Regardless of whether you specify a custom anchor pattern, you must supply a 126 matching named anchor at a suitable place in your template. 127 128 For example:: 129 130 {% for comment in comment_list %} 131 <a name="c{{ comment.id }}"></a> 132 <a href="{% get_comment_permalink comment %}"> 133 permalink for comment #{{ forloop.counter }} 134 </a> 135 ... 136 {% endfor %} 137 138 .. warning:: 139 140 There's a known bug in Safari / webkit which causes the named anchor to be 141 forgotten following a redirect. The practical impact for comments is that 142 the Safari/webkit browsers will arrive at the correct page but will not 143 scroll to the named anchor. 144 107 145 .. templatetag:: get_comment_count 108 146 109 147 Counting comments -
tests/regressiontests/comment_tests/tests/templatetag_tests.py
diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py index a1187ca..ea68e67 100644
a b class CommentTemplateTagTests(CommentTestCase): 63 63 64 64 def testGetCommentListFromObject(self): 65 65 self.testGetCommentList("{% get_comment_list for a as cl %}") 66 67 def testGetCommentPermalink(self): 68 self.createSomeComments() 69 t = "{% load comments %}{% get_comment_list for comment_tests.author a.id as cl %}" 70 t += "{% get_comment_permalink cl.0 %}" 71 ctx, out = self.render(t, a=Author.objects.get(pk=1)) 72 self.assertEqual(out, "/cr/13/1/#c2") 73 74 def testGetCommentPermalinkFormatted(self): 75 self.createSomeComments() 76 t = "{% load comments %}{% get_comment_list for comment_tests.author a.id as cl %}" 77 t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}" 78 ctx, out = self.render(t, a=Author.objects.get(pk=1)) 79 self.assertEqual(out, "/cr/13/1/#c2-by-Joe Somebody") 80 81