0

我只是对 django 评论中的一个奇怪的属性感到好奇。当发表评论后(在/comments/post/),django 将重定向到 ?c="comment_pk"。来自 django 的评论库:

"""
Handle the "where should I go next?" part of comment views.

The next value could be a kwarg to the function (``default``), or a
``?next=...`` GET arg, or the URL of a given view (``default_view``). See
the view modules for examples.

Returns an ``HttpResponseRedirect``.
"""
next = data.get("next", default)
if next is None:
    next = urlresolvers.reverse(default_view)
if get_kwargs:
    if '#' in next:
        tmp = next.rsplit('#', 1)
        next = tmp[0]
        anchor = '#' + tmp[1]
    else:
        anchor = ''

    joiner = ('?' in next) and '&' or '?'
    next += joiner + urllib.urlencode(get_kwargs) + anchor
return HttpResponseRedirect(next)

我只是好奇为什么 django 的制造商决定拥有这个。只是重定向回同一页面有什么问题?是否真的需要评论 pk 参数?在我看来不是。

此外,当我尝试将 httpresponse 修改为 '' 空字符串的值时,由于某些init期望 2 个参数错误,它不会让我这样做。如果这 ?c=pk 真的不需要,我怎样才能摆脱这个自动重定向参数?

谢谢!

4

1 回答 1

0

我一直在使用评论框架来获得 facebook 风格的评论,它会重定向到同一页面,如下所示。注意将表单重定向到同一页面的方式:

<input type="hidden" name="next" value="/your/redirect/url"/>

{% get_comment_form for update as form %}

                                    <div class="commentForm">

                                      <form action="/comments/post/" method="post">
                                        {% csrf_token %}
                                         <textarea class="commentBox" id="id_comment" rows="10" cols="40" name="comment"></textarea>


                                        <input id="id_name" type="hidden" name="name" value="{{user.username}}" maxlength="50"/>
                                        <input type="hidden" name="email" id="id_email" value="{{user.email}}"/>
                                        <input type="hidden" name="url" id="id_url" value=""/>
                                        <input type="hidden" name="honeypot" id="id_honeypot" value=""/>
                                        <input type="hidden" name="next" value="/your/redirect/url/"/>

                                            {% for hidden in form.hidden_fields %}
                                              {{hidden}}
                                            {% endfor %}

                                        <button class="btn comment-btn" type="submit" name="submit"> Comment</button>


                                    </form>
于 2012-09-12T07:37:15.243 回答