Django:发布评论后重定向 [英] Django: Redirect after posting a comment

查看:124
本文介绍了Django:发布评论后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户重定向到发布评论的页面。我发现这个在Django的网站上张贴但是我做错了,因为它不会重定向回来。

I am trying to redirect the user back to the page where the comment was posted. I found this post on Django's site but I am doing something wrong because it won't redirect back.

输入的位置应该在哪里妥善重定向?

Where should the input be placed to have it properly redirected?

{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
  {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
  {% for field in form %}
    {% if field.is_hidden %}
      {{ field }}
    {% else %}
      {% if field.errors %}{{ field.errors }}{% endif %}
            <input type="hidden" name="next" value="{% url proposal proposal.id %}" />
      <p
        {% if field.errors %} class="error"{% endif %}
        {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "name" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "email" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "url" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "title" %} style="display:none;"{% endifequal %}>
        <!-- {{ field.label_tag }}  -->{{ field }}
      </p>
    {% endif %}
  {% endfor %}
  <p class="submit">
    <!-- <button><input type="submit" name="post" value="{% trans "Send" %}" /></button> -->
        <button type="submit">Send</button>
    <!-- <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" /> -->
  </p>
</form>


推荐答案

也许你不需要检查下一个变量。您可以尝试更改:

Maybe you don't need to check for next variable in your template. You could try changing:

{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}

至:

<input type="hidden" name="next" value="/added/comment/page/" />

如果您使用views.py,从那里重定向似乎更明显,至少对我而言它有助于保持远离模板:

In case you use views.py, redirecting from there seems more obvious, at least for me, as it helps keep the concern away from the template:

from django.http import HttpResponseRedirect
HttpResponseRedirect("/path/to/redirect")

这篇关于Django:发布评论后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆