I am trying to render a form where more then one field are initialized dynamically based on a parameter passed from view to form. I am setting the initial value at the form's init method and it seems to work fine as far as assigning the values to field elements is considered. When I print the form, I can see those values attached to appropriate field element. The issue is that those values does not show up when rendered.
My View is:
def zoneinfo(request):
siteId = request.GET.get('siteId')
form = PostForm(site = siteId.upper())
print form
return render(request, 'details.html', {'form': form})
code in form.py
def __init__(self, *args, **kwargs):
siteId = kwargs.pop('site', None)
super(PostForm, self).__init__(*args, **kwargs)
if siteId:
siteInfo = getSiteInfo(siteId.upper())
self.fields['city'] = forms.CharField(max_length=25, initial=siteInfo['city'])
self.fields['address'] = forms.CharField(max_length=25, initial=siteInfo['address'])
self.fields['state'] = forms.CharField(max_length=50, initial=siteInfo['state'])
self.fields['zip'] = forms.CharField(max_length=10, initial=siteInfo['zip'])
details.html:
{% load staticfiles %}
<form action = "/zones/add" id="addNewZoneForm1" method='post'>{% csrf_token %}
<!-- <form id="addNewSite" method='post'>{% csrf_token %} -->
<fieldset>
<table border="0" cellspacing="2" cellpadding="2" width="100%">
{{ form.as_table }}
<!-- Allow form submission with keyboard without duplicating the dialog button -->
</table>
<input type="submit" value="Submit">
</fieldset>
</form>
<script type="text/javascript">
function getSite(value){
$.ajax({
url: "sites/getinfo?siteId="+value,
type: "get",
})
}
</script>
where Print form returns :
<tr><th><label for="id_site">Site:</label></th><td><select id="id_site" name="site" onChange="getSite(this.value);">
<option value="initialValue">----Select Site----</option>
<option value="abc">ABC</option>
<option value="xyz">XYZ</option>
</select></td></tr>
<tr><th><label for="id_city">City:</label></th><td><input id="id_city" maxlength="25" name="city" type="text" value="SAN" /></td></tr>
<tr><th><label for="id_address">Address:</label></th><td><input id="id_address" maxlength="25" name="address" type="text" value="SAN" /></td></tr>
<tr><th><label for="id_state">State:</label></th><td><input id="id_state" maxlength="50" name="state" type="text" value="SAN" /></td></tr>
<tr><th><label for="id_zip">Zip:</label></th><td><input id="id_zip" maxlength="10" name="zip" type="text" value="10000001" /></td></tr>
<tr><th><label for="id_description">Description:</label></th><td><textarea cols="40" id="id_description" name="description" rows="10">
</textarea></td></tr>
We can see in the html out put that the fields have values associated but they are not rendered in the view. There is no error that I am getting, just a form with empty fields
EDIT: I tired to print return render(request, 'details.html', {'form': form})
on console and the output contains the values for the fields. Not sure why those values are not visible on the web page.
Thanks in advance for your help