I’m new to Django and I have a page with several forms and submit buttons. How can I store the data on the fields of the other forms if one is submitted? Normally when I submit one, it redirects to the same page but if something was typed on the fields is lost.
Thanks.
This is the Html
<head>
<title>Hello</title>
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}/static/tabs.css" />
</head>
<body>
<h1>Informacion</h1>
<ol id="toc">
<li><a href="#page-1"><span>Page 1</span></a></li>
<li><a href="#page-2"><span>Page 2</span></a></li>
<li><a href="#page-3"><span>Page 3</span></a></li>
<li><a href="#page-4"><span>Page 4</span></a></li>
</ol>
<div class="content" id="page-1">
<h2>Page 1</h2>
<p>Text...</p>
<form action="." method="POST">{% csrf_token %}
<table>
{{ form1.as_table }}
</table>
<p><input type="submit" value="Submit"></p>
</form>
</div>
<div class="content" id="page-2">
<h2>Page 2</h2>
<p>Text...</p>
<form action="." method="POST">{% csrf_token %}
<table>
</table>
<p><input type="submit" value="Submit"></p>
</form>
</div>
<div class="content" id="page-3">
<h2>Page 3</h2>
<p>Text...</p>
<form action="." method="POST">{% csrf_token %}
<table>
{{ form2.as_table}}
</table>
<p><input type="submit" value="Submit"></p>
</form>
</div>
<div class="content" id="page-4">
<h2>Busqueda</h2>
<form action="." method="POST">{% csrf_token %}
<table>
{{ form2.as_table}}
</table>
<p><input type="submit" value="S"></p>
</div>
<script src="{{ MEDIA_URL}}/static/activatables.js" type="text/javascript"></script>
<script type="text/javascript">
activatables('page', ['page-1', 'page-2', 'page-3','page-4']);
</script>
</body>
Well, there are some tabs made in java script for each different form, so what I want to do is that when you submitt data for one form, the other data in the fields of the rest of the forms should be kept, so the user shouldnt type it again or lose it. Thanks in advance.
in the view…
if request.method == 'GET':
form1 = HcGinecoForm()
form2 = HolaForm()
else:
form1 = HcGinecoForm(request.POST)
form2 = HolaForm(request.POST)
if form1.is_valid and form2.is_valid():
form1.save()
form2.save()
return render_to_response('polls/hola.html',{
'form2':form2,
'form1':form1
})
It’s working so far… thanks.
In order to persist the values from multiple forms from one POST, you’ll need to have all of the fields in one form (without using JavaScript).
In your view action, you would need to validate each form, and then call it’s save method.
When posting to this action, the values input into any of these forms will be preserved after the post. If you want the values to not be preserved, you’ll need to create new instances of the forms that aren’t populated from the request.POST collection.