Reputation: 111
I'm having problems because every value in request.POST is inside a list. So whenever I do this:
MyForm(request.POST)
None of the validations pass because they are expecting strings and not lists. And the error messages are of this sort:
[u'13/04/2000'] is not a valid date
Is there a setting or something i need to change so i can just pass request.POST to a form? I really don't want to do something like request.POST.get(..) for every field.
Ok here is my form:
class FormAddCourse(forms.Form):
career = choice_field(Career)
assignature = choice_field(Assignature)
start_date = forms.DateField(label = spanish('col_name', 'start_date', True),
help_text = "Formato: dd/mm/aaaa")
end_date = forms.DateField(label = spanish('col_name', 'end_date', True),
help_text = "Formato: dd/mm/aaaa")
day_choices = [('Mo', 'Lunes'), ('Tu', 'Martes'), ('We', 'Miércoles'), ('Th', 'Jueves'),
('Fr', 'Viernes'), ('Sa', 'Sábado'), ('Su', 'Domingo')]
days = forms.MultipleChoiceField(label = spanish('col_name_plural', 'day', True),
widget = CheckboxSelectMultiple,
choices = day_choices)
length_choices = (
('S', spanish('choices', 'semesterly', True)),
('Y', spanish('choices', 'yearly', True)),
)
length = forms.ChoiceField(label = spanish('col_name', 'length', True),
widget = RadioSelect,
choices = length_choices)
hours = forms.CharField(widget = HiddenInput,
max_length = 300,
validators=[validate_hours])
And here is my view:
def add_course_view(request):
if request.method == "GET":
form = FormAddCourse()
c = {'form': form}
return render_to_response('crud/courses/add.html', c, RequestContext(request))
elif request.method == "POST":
try:
hours = get_hours(request.POST)
form_data = {'hours': hours}
form_data.update(request.POST.copy())
form = FormAddCourse(form_data)
if form.is_valid(): # This thing never passes
career = form.cleaned_data['career']
assignature = form.cleaned_data['assignature']
start_date = form.cleaned_data['start_date']
end_date = form.cleaned_data['end_date']
days = form.cleaned_data['days']
length = form.cleaned_data['length']
hours = form.cleaned_data['hours']
credits = calculate_credits(valid_hours)
# alter database
# course = Course.objects.create(career=career, assignature=assignature, start_date=start_date, end_date=end_date, days=days, length=length, credits=credits, hours=hours)
if u'submit_another' in request.POST:
# Submit and add another
messages.add_message(request, messages.SUCCESS, u"El curso ha sido guardado. Agregue otro.")
return redirect('crud.views.add_course_view')
else:
# Submit and end
messages.add_message(request, messages.SUCCESS, u"El curso ha sido guardado.")
return redirect('ssys.views.homepage')
except FieldError as e:
# return user to complete form with fields already filled
pass
As for the validation functions, the only custom one is validate_hours which is actually the only one that passes because I add in the hours manually to the form_data.
Ok i figured what the problem is, I can append to request.POST.copy() but i can't append it to a dictionary. Thank you all for your help.
Upvotes: 1
Views: 953
Reputation: 122456
You should be instantiating the form as follows:
form = MyForm(request.POST)
if form.is_valid():
pass
No other code should be needed to get basic form validation in Django. If you need to add values to request.POST
then consider creating hidden input fields and including the intended values in the form already.
Upvotes: 0
Reputation: 23871
request.POST
is a QueryDict, which is dictionary-like class but not dictionary.
You could try
form_data = request.POST.copy()
form_data.update({'hours': hours})
#instead of
form_data = {'hours': hours}
form_data.update(request.POST.copy())
But, according to your code, the hours
is fetched from request.POST
also. Then why not merely use the form to process all fields? If you have trouble to do it in the form or there is any special logic consideration, you could make it clear in the question.
Upvotes: 3