I'm having a strange problem where I can't seem to set the initial value of one of the fields in my forms in django.
My model field is:
section = models.CharField(max_length=255, choices=(('Application', 'Application'),('Properly Made', 'Properly Made'), ('Changes Application', 'Changes Application'), ('Changes Approval', 'Changes Approval'), ('Changes Withdrawal', 'Changes Withdrawal'), ('Changes Extension', 'Changes Extension')))
My form code is:
class FeeChargeForm(forms.ModelForm):
class Meta:
model = FeeCharge
# exclude = [] # uncomment this line and specify any field to exclude it from the form
def __init__(self, *args, **kwargs):
super(FeeChargeForm, self).__init__(*args, **kwargs)
self.fields['received_date'] = forms.DateField(('%d/%m/%Y',), widget=forms.DateTimeInput(format='%d/%m/%Y', attrs={'class': 'date'}))
self.fields['comments'].widget.attrs['class']='html'
self.fields['infrastructure_comments'].widget.attrs['class']='html'
My view code is:
form = FeeChargeForm(request.POST or None)
form.fields['section'].initial = section
Where section is a url var passed to the function. I've tried:
form.fields['section'].initial = [(section,section)]
With no luck either :(
Any ideas what I'm doing wrong or is there a better way to set the default value (before a form submit) of this choice field from a url var?
Thanks in advance!
Update: It seems to be something to do with the URL variable.. If I use:
form.fields['section'].initial = "Changes Approval"
It works np.. If I HttpResponse(section) it's outputs correctly tho.