On Tuesday, April 5, 2011 9:43:48 PM UTC+1, Roy Smith wrote:
>
> I'm using django-1.3 .  I have a view with the following code: 
>
> def item_create(request): 
>     if request.method == 'POST': 
>         form = ItemForm(request.POST) 
>         if form.is_valid(): 
>             url = form.cleaned_data['url'] 
>             item.save() 
>             return HttpResponseRedirect('/') 
>         else: 
>             print form.errors 
>
> when I submit the form, I expected that form.errors would print out as 
> a dict, as documented in 
> http://docs.djangoproject.com/en/1.3/ref/forms/api/#using-forms-to-validate-data.
>  
>
> Instead, I'm getting a hunk of HTML: 
>
>
> Django version 1.3, using settings 'soco-site.settings' 
> Development server is running at http://0.0.0.0:7626/ 
> Quit the server with CONTROL-C. 
> <ul class="errorlist"><li>date_added<ul class="errorlist"><li>This 
> field is required.</li></ul></li><li>user_id<ul 
> class="errorlist"><li>This field is required.</li></ul></li></ul> 
> [05/Apr/2011 16:36:32] "POST /item/create/ HTTP/1.1" 200 718 
>
> Is my understanding wrong, or is this a bug? 
>


To coin a phrase, it's not a bug - it's a feature.

form.errors is actually an instance of a custom class, ErrorDict, which is 
defined in django.forms.util. It subclasses dict, so it works exactly the 
same as a dictionary when you set and access its members - but it also 
defines a custom __unicode__ method, which is called automatically when you 
`print` it, which converts the contents to HTML. This is so you get a 
nicely-formatted output in your template.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to