Django:发表评论后重定向

标签 django django-forms django-comments

我试图将用户重定向回发表评论的页面。我找到了这个 post on Django's site但我做错了什么,因为它不会重定向回来。

输入应该放在哪里才能正确重定向?

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

最佳答案

也许你不需要检查 下一个 模板中的变量。你可以尝试改变:

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

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

如果您使用 views.py,从那里重定向似乎更明显,至少对我来说,因为它有助于使关注远离模板:
from django.http import HttpResponseRedirect
HttpResponseRedirect("/path/to/redirect")

关于Django:发表评论后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5169715/

相关文章:

django - Tornado 和 Django 的限制?

css - 我如何在 Django 中使用 CSS?

django评论框架: distinct() does not work?

python - django-rest-framework 接受 JSON 数据?

linux - tail 文件 - 该文件变得不可访问然后又出现了

django - 如何使用 brew 安装 Django?

python - 如何从 Django 注册表单中的密码字段获取纯密码

python - Django:如何显示选项的名称而不是整数?

django - 在新的 Django 项目中使用 django-contrib-comments(应用程序)?

Django zinnia 禁用评论或使用替代方案