返回上一页(取消行为)

Back to previous page (cancel behavior)

我想在注销页面中添加一个按钮以取消该过程。喜欢这里:

<h1 class="h1">{% trans "Sign Out" %}</h1>

<p class="lead">{% trans 'Are you sure you want to sign out?' %}</p>

<form id="logout_form" class="logout" method="post" action="{% url 'account_logout' %}">
  {% csrf_token %}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
  <a class="btn btn-danger btn-block" type="submit" href="#?">{% trans 'Cancel' %}</a>
  <button class="btn btn-danger btn-block" type="submit">{% trans 'Sign Out' %}</button>
</form>

我想如果用户单击“取消”,他们会返回上一页,他们来自哪里。

你可以这样尝试 request.META.HTTP_REFERER:

<form id="logout_form" class="logout" method="post" action="{% url 'account_logout' %}">
  {% csrf_token %}
  {% if request.META.HTTP_REFERER %}
  <a class="btn btn-danger btn-block" href="{{ request.META.HTTP_REFERER }}">{% trans 'Cancel' %}</a>
  {% endif %}
  <button class="btn btn-danger btn-block" type="submit">{% trans 'Sign Out' %}</button>
</form>