1
I am developing a web application using Python and Django, and at some point I need the page to be updated, but without losing the information that was typed in a form.
I tried to use the script
below, but even updating the page, all information is reset.
<script>
window.onbeforeunload = function() {
localStorage.setItem("name", $('#id_cliente_form-nome_razao_social').val());
localStorage.setItem("tipopessoa", $('input[name=cliente_form-tipo_pessoa]').val());
// ...
};
window.onload = function() {
var name = localStorage.getItem("name");
var tipopessoa = localStorage.getItem("tipopessoa");
if (name !== null) $('#id_cliente_form-nome_razao_social').val("name");
if (tipopessoa !== null) $('input[name=cliente_form-tipo_pessoa]').val("tipopessoa");
// ...
}
</script>
My file py views. is like this:
class AdicionarClienteView(AdicionarPessoaView):
template_name = "cadastro/pessoa_add.html"
def get_context_data(self, **kwargs):
context = super(AdicionarClienteView, self).get_context_data(**kwargs)
return context
def get(self, request, *args, **kwargs):
form = ClienteForm(prefix='cliente_form')
return super(AdicionarClienteView, self).get(request, form, *args, **kwargs)
def post(self, request, *args, **kwargs):
form = ClienteForm(request.POST, request.FILES,
prefix='cliente_form', request=request)
return super(AdicionarClienteView, self).post(request, form, *args, **kwargs)
And my file of template HTML like this:
<div class="col-sm-12" style="margin-top:15px;">
<div class="form-group">
<div class="form-line">
<input type="radio" name="{{form.prefix}}-tipo_pessoa" id="fisica" value="PF" {% if form.tipo_pessoa.value == 'PF' %}checked="checked"{% endif %}/>
<label for="fisica"><strong>Pessoa Física</strong></label>
<input type="radio" name="{{form.prefix}}-tipo_pessoa" id="juridica" value="PJ" {% if form.tipo_pessoa.value == 'PJ' %}checked="checked"{% endif %}/>
<label for="juridica"><strong>Pessoa Jurídica</strong></label>
<strong style="color:red;"> *</strong>
{% if form.tipo_pessoa.errors %}<label class="error">{% for error in form.tipo_pessoa.errors %}{{error}}{% endfor %}</label>{% endif %}
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<div class="form-line">
<label>{{form.nome_razao_social.label}}</label><strong style="color:red;"> *</strong>
{% if form.nome_razao_social.errors %}<label class="error">{% for error in form.nome_razao_social.errors %}{{error}}{% endfor %}</label>{% endif %}
{{form.nome_razao_social}}
</div>
</div>
</div>
// ...