When I submit this form & all fields are correctly populated, the form.is _valid() returns false & all the fields give : this field is required error, even the CharField!!!
can anybody see what's wrong? this is my form:
class TemplateConfiguredForm(forms.Form):
"""This form represents the TemplateConfigured Form"""
template = forms.ChoiceField(widget=forms.Select(attrs={'id':'TemplateChoice'}))
logo = forms.ImageField( widget = forms.FileInput(attrs={'id': 'inputLogo'}))
image = forms.ImageField(widget = forms.FileInput(attrs={'id': 'inputImage'}))
message = forms.CharField(widget = forms.Textarea(attrs={'id': 'inputText', 'rows':5, 'cols':25}))
def __init__(self, custom_choices=None, *args, **kwargs):
super(TemplateConfiguredForm, self).__init__(*args, **kwargs)
r = requests.get('http://127.0.0.1:8000/sendMails/api/templates/?format=json')
json = r.json()
custom_choices=( ( template['url'], template['name']) for template in json)
if custom_choices:
self.fields['template'].choices = custom_choices
this my template:
<form id="template_form" method="post" role="form" enctype="multipart/form-data" action="{% url 'create_templates' %}" >
{% csrf_token %}
{{ form.as_p }}
{% buttons %}
<input type="submit" value="Save Template"/>
{% endbuttons %}
</form>
this is my view:
def create_templates(request):
if request.method == 'POST':
form = TemplateConfiguredForm(request.POST, request.FILES)
if form.is_valid():
template_configured = TemplateConfigured()
template_configured.owner = request.user
template_configured.logo = form.cleaned_data["logo"]
template_configured.image = form.cleaned_data["image"]
template_configured.message = form.cleaned_data["message"]
template = form.cleaned_data['template']
template = dict(form.fields['template'].choices)[template]
template_configured.template = Template.objects.get(name = template)
template_configured.save()
saved = True
else:
print form.errors
else:
form = TemplateConfiguredForm()
return render(request, 'sendMails/createTemplates.html', locals())
The data you pass in your form, here:
form = TemplateConfiguredForm(request.POST, request.FILES)
is captured by the first keyword argument of your signature:
def __init__(self, custom_choices=None, *args, **kwargs):
Remove the custom_choices=None