Django Comment,在 url 注释中附加符号?

发布于 2024-08-05 06:59:28 字数 547 浏览 4 评论 0原文

我正在使用评论系统,现在,我想重写 url 评论中的段并附加符号 #,我想将页面部分移动到评论列表中,精确到最后一个评论用户 <名称=#{{comment.id}}?>用户名

我在发布评论时使用 next 来重定向用户:

{% 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>

但是在 Django 文档中,没有提及重写或定制评论重定向/url

有什么想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-08-12 06:59:28

我只是偶然发现了这一点丑陋。阅读源代码后,我没有看到任何好的方法来覆盖此行为。默认情况下,您将被重定向到模板的 {{ next }} 变量中的 URL,并且 Django 将 ?c=1 附加到 1 所在的 URL code> 是评论的 ID。我希望将其改为 #c1,以便用户从页面跳转到他们刚刚发布的评论。我通过一些“猴子修补”来完成此操作,如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文