Re: form validation in XMLHttp submission

2009-02-05 Thread adrian

It is true that I was accessing the error message incorrectly in
python - it should be
my_form.errors['date'] and it is set.   However the doc says you can
access that same value with form.name_of_field.errors in a template
like this:



{{ form.date.errors }}
Date:
{{ form.date}}

But it is not showing up in my view.  I also tried form.errors.date
but that does not work either.

Any help appreciated.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: form validation in XMLHttp submission

2009-02-04 Thread Malcolm Tredinnick

On Wed, 2009-02-04 at 11:23 -0800, adrian wrote:
> 
> 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.  

The error message is correct. Your form has a form field called "date",
but the form class does not have an *attribute* called date. The form
fields are stored in an attribute on the form called "fields".

So my_form.field["date"] will access the date form field.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



form validation in XMLHttp submission

2009-02-04 Thread adrian


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:
-MM-DD.")

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:
Date:Enter a valid date.Please use the
following format: -MM-DD.

form errors found: form.errorsdateEnter a valid date.
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
-~--~~~~--~~--~--~---