Django Forms - Custom error messages

945 views
Skip to first unread message

Weslei A. de T. Marinho

unread,
Jul 20, 2008, 11:39:40 PM7/20/08
to google-a...@googlegroups.com
Hello,

I want to use this feature:

http://www.djangoproject.com/documentation/newforms/#error-messages

But when I do:

class InsertUserForm(djangoforms.ModelForm):
name = forms.CharField(error_messages={'required': 'Please enter
your name'})

it comes up with the following error:

TypeError: __init__() got an unexpected keyword argument 'error_messages'

so, how to do this with current django version of google app engine?
How to make a custom error message to show up in the django form? I
don't want to rewrite the validation rules by rewriting them at the
clean_fieldname methods, I just want to replace the default
messages...

Thanks in advance.

--
Weslei A. de T. Marinho

Calvin Spealman

unread,
Jul 21, 2008, 12:21:01 AM7/21/08
to google-a...@googlegroups.com
Wrap the request handler and catch all exceptions, and handle by
displaying your own page.

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/

g-man

unread,
Jul 21, 2008, 11:36:25 PM7/21/08
to Google App Engine
OK, if you're talking about a custom validator for a Django form,
here's how I did it:

-----

# google imports:
from google.appengine.ext.db import djangoforms

# django imports:
from django import newforms as forms
from django.newforms.util import ValidationError

# app imports:
from blog.models.tag_model import Tag

class TagForm(djangoforms.ModelForm):
def clean_name(self):
target=self.clean_data['name']
if not self.instance:
if Tag.all().filter('name = ', target).get():
raise ValidationError, '...name must be unique'
return target

class Meta:
model=Tag

-----

All it does is look to see if any existing Tag has the same name, then
raises a nice ValidationError before you leave the input form. Django
runs clean_fieldname() test for any validation you want to define this
way (I have a whole bunch for each model). The model assignment in the
Meta subclass defines the fields the form will display for input (with
certain exceptions, like 'id', etc).

My learning app is at:

http://archi-checker.appspot.com

... but I think this feature is hidden behind my 'admin' curtain, but
I use it for both the blog Item title and Tag name to test for
'unique' (which App Engine does not do yet, as it does for
'required=True').

Tudo bein!


On Jul 20, 8:39 pm, "Weslei A. de T. Marinho" <wes...@gmail.com>
wrote:

Weslei A. de T. Marinho

unread,
Jul 22, 2008, 7:28:45 AM7/22/08
to google-a...@googlegroups.com
That is the point... I don't want to rewrite all the validations. I
just want to replace the error-messages, because my application is in
another language than english.

g-man

unread,
Jul 22, 2008, 3:17:15 PM7/22/08
to Google App Engine
Well, didn't the example just define an error message as:

if Tag.all().filter('name = ', target).get():
raise ValidationError, '...es necessario la palabra esta
uniqua '

... and there is also the translation system i18n or whatever.

So, the problem is that the vanilla App Engine validators do not work
yet, as far as I can see, (except for required=True), so I just use
the Django ones. If you know how to get the Google ones to work
through the Model definistion, please let us know, because I can't
find it anywhere.


On Jul 22, 4:28 am, "Weslei A. de T. Marinho" <wes...@gmail.com>
wrote:
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages