浏览器中的后退按钮

浏览器中的后退按钮

本文介绍了HttpResponseRedirect和CreateView:浏览器中的后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpResponseRedirect可以防止在浏览器中返回并再次提交表单吗?CreateView使用HttpResponseRedirect对吗?如果是,那么我对自己所拥有的感到困惑.通过使用此视图和模板,我注意到我可以返回浏览器并一次又一次提交表单...该字段的值仍然存在.

Does HttpResponseRedirect prevent from going back on the browser and submit the form again?CreateView uses HttpResponseRedirect right?If yes, then I am confused about what I have.By using this view and template, I noticed that I can go back on my browser and submit the form again and again... The value of the field being still there.

from .models import Person
class PersonCreateView(CreateView):
    model = Person
    fields = ['first_name']
    template_name = 'persons/create.html'
    success_url = '/'

和模板:

{% extends "base.html" %}
{% block content %}
<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Create" />
</form>
{% endblock %}

Django教程的第4部分说:

The Django tutorial Part 4 says:

# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.

但是我看不到这种情况,有什么不同.

but I do not see this behavior in my case, what is different.

推荐答案

"HttpResponseRedirect是否可以阻止浏览器返回并再次提交表单?"

"Does HttpResponseRedirect prevent from going back on the browser and submit the form again?"

->这是它很好的用途之一.

-> this is one of its nice use.

您的问题是您忘记了表单模板中的 {%csrf_token%} ,并且可能存在错误.

Your problem is that you have forgotten {% csrf_token %} in your form template, and there's problably an error.

此外,我不知道 {{form.as_p}} 是否显示错误,请尝试进行循环:

Moreover I dont know if {{ form.as_p }} displays the errors, try to make a loop:

{% for field in form.visible_fields %}
    {{ field }}
    {% if field.errors %}
        <ul>
            {% for error in field.errors %}
                <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endfor %}
{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}

这篇关于HttpResponseRedirect和CreateView:浏览器中的后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 09:44