I'm using the standard pattern for handling a POST request form submission, even though the form is submitted by JavaScript as an XMLHttpRequest, and the date validation is not working. I added some logging statements to find the problem and I get the error message "MyModelForm' object has no attribute 'date' " even though it definitely has that attribute, as shown in the logs below. Django is generating the error message for that field in form.errors, but it never gets put into the field-specific errors such as form.date.errors. Does anyone know why?
Here's the model and form definition in models.py: class MyModel(models.Model): .... date = models.DateField(help_text="Please use the following format: <em>YYYY-MM-DD</em>.") .... class MyModelForm(ModelForm): class Meta: model = MyModel exclude = ('user', 'date_added', 'active') And here's the view: def addtest(request, region): if request.method == 'POST': # form submitted or ajax data initial load.. form = MyModelForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # add entry to database here else: # initial ajax load or error if 'loadAll' in request.POST: #ajax load logging.debug( "initial ajax load") else: logging.debug( "form errors found, form is: %s" % form) logging.debug( "form errors found: form.errors%s" % form.errors) logging.debug( "form.date.errors: %s" % form.date.errors) Here is the log output (irrelevant stuff omitted): form errors found, form is:.... <tr><th><label for="id_date">Date:</label></th><td><ul class="errorlist"><li>Enter a valid date.</li></ul><input type="text" name="date" value="02//2009" id="id_date" /><br />Please use the following format: <em>YYYY-MM-DD</em>.</td></tr> .... form errors found: form.errors<ul class="errorlist"><li>date<ul class="errorlist"><li>Enter a valid date.</li></ul></li></ul> INSERT INTO `crashlog_error` (`class_name`, `message`, `traceback`, `datetime`, `url`, `server_name`) VALUES (AttributeError, 'MyModelForm' object has no attribute 'date', Traceback (most recent call last): File "C:\Python25\Lib\site-packages\django\core\handlers\base.py", line 86, in get_response response = callback(request, *callback_args, **callback_kwargs) File "C:\....\myproject\lister\views.py", line 244, in addtest logging.debug( "form.date.errors: %s" % form.date.errors) AttributeError: 'MyModelForm' object has no attribute 'date' , 2009-02-03 15:09:34, http://127.0.0.1:8000/lister/addtest/WA/, D602ZZ71) .... `traceback` = Traceback (most recent call last): File "C:\Python25\Lib\site-packages\django\core\handlers\base.py", line 86, in get_response response = callback(request, *callback_args, **callback_kwargs) File "C:\Documents and Settings\Adrian Nye\My Documents\BirdList \myproject\lister\views.py", line 244, in addtest logging.debug( "form.date.errors: %s" % form.date.errors) AttributeError: 'MyModelForm' object has no attribute 'date' , `times_seen` = 3, `last_seen` = 2009-02-03 14:42:59, `first_seen` = 2009-02-03 14:42:59, `url` = http://127.0.0.1:8000/lister --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---