List Info

Thread: Forms and formating error messages




Forms and formating error messages
country flaguser name
United States
2007-06-25 10:22:27
Hi,
I was trying to change the way in which the errors are
presented in a
form.
I tried to put them in:
<tr><td
align="center"><b>{{field.label}}:</b&
gt;</td><td>{}{% if
field.field.required %}<span
class="highlight">*</span>{%endif%}</
td>{%if field.errors%}<td><span
class="highlight">{{field.errors}}</
span></td>{%endif%}</tr>
but I still get them formatted as an <ul> and I would
like the errors
to appear as simple text..
I tried these instructions in this page:
http
://code.djangoproject.com/wiki/TemplatedForm
But I can't seem to make it work..
I have this error on my models.py: Exception Value:  	name
'TemplatedForm' is not defined
I understand that I have to import something, I just don't
know how to
import it...
And by the way, this thing described on the web-page I spoke
about is
for this purpose right? Changing the appearance of the
errors?


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


Re: Forms and formating error messages
country flaguser name
Australia
2007-06-25 22:09:07
On Mon, 2007-06-25 at 08:22 -0700, AnaReis wrote:
> Hi,
> I was trying to change the way in which the errors are
presented in a
> form.
> I tried to put them in:
> <tr><td
align="center"><b>{{field.label}}:</b&
gt;</td><td>{}{% if
> field.field.required %}<span
class="highlight">*</span>{%endif%}</
> td>{%if field.errors%}<td><span
class="highlight">{{field.errors}}</
> span></td>{%endif%}</tr>
> but I still get them formatted as an <ul> and I
would like the errors
> to appear as simple text..

I'm beginning to suspect this is a slight design flaw in
newforms: the
presentation of error messages is hard-coded into the way
the ErrorList
class (in newforms.utils) is used in the HTML rendering.

I can think of a couple of backwards-compatible ways we
might be able to
change this. I'll bounce some ideas of the developers list.

> I tried these instructions in this page:
> http
://code.djangoproject.com/wiki/TemplatedForm
> But I can't seem to make it work..
> I have this error on my models.py: Exception Value: 
	name
> 'TemplatedForm' is not defined
> I understand that I have to import something, I just
don't know how to
> import it...

You have to import TemplatedForm from whichever file you
defined it in.
Both TemplatedForm and the new form subclass you are
creating are just
classes in your Python code. Import them as you would any
othre class in
Python.

Regards,
Malcolm

-- 
How many of you believe in telekinesis? Raise my hand... 
http://www.pointy-s
tick.com/blog/


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


Re: Forms and formating error messages
country flaguser name
United States
2007-06-26 06:22:58


On Jun 26, 3:09 am, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Mon, 2007-06-25 at 08:22 -0700, AnaReis wrote:
> > Hi,
> > I was trying to change the way in which the errors
are presented in a
> > form.
> > I tried to put them in:
> > <tr><td
align="center"><b>{{field.label}}:</b&
gt;</td><td>{}{% if
> > field.field.required %}<span
class="highlight">*</span>{%endif%}</
> > td>{%if field.errors%}<td><span
class="highlight">{{field.errors}}</
> > span></td>{%endif%}</tr>
> > but I still get them formatted as an <ul>
and I would like the errors
> > to appear as simple text..
>
> I'm beginning to suspect this is a slight design flaw
in newforms: the
> presentation of error messages is hard-coded into the
way the ErrorList
> class (in newforms.utils) is used in the HTML
rendering.
>
> I can think of a couple of backwards-compatible ways we
might be able to
> change this. I'll bounce some ideas of the developers
list.
>
> > I tried these instructions in this page:
> >http
://code.djangoproject.com/wiki/TemplatedForm
> > But I can't seem to make it work..
> > I have this error on my models.py: Exception
Value:        name
> > 'TemplatedForm' is not defined
> > I understand that I have to import something, I
just don't know how to
> > import it...
>
> You have to import TemplatedForm from whichever file
you defined it in.
> Both TemplatedForm and the new form subclass you are
creating are just
> classes in your Python code. Import them as you would
any othre class in
> Python.
>
> Regards,
> Malcolm
>
> --
> How many of you believe in telekinesis? Raise my
hand...http://www.pointy-s
tick.com/blog/

I wrote another response but it's been 2h or more and it
doesn't show
here so I'll post it again... Sorry if this is repeated...

I'm kind of a noob on these python things and I don't know
very well
how to handle these path things.
The file where TemplatedForm is, newforms.py, is in this
directory:
/nobackup/reis/Project/Manager/utils

The import on my file is:
from Manager.utils.newforms import TemplatedForm

I have this error:
Exception Value:  	No module named utils.newforms

What am I doing wrong and why? Do I have to put something in
the
settings file?
Sorry for the lameness...
Regards.


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


Re: Forms and formating error messages
country flaguser name
United States
2007-06-26 10:15:52
Hum... I tried to test the imports on IDLE but I couldn't
import the
file in anyway I tried..
I went to the  Path Browser on IDLE and I couldn't find the
newforms.py file that I created. I then noticed that in all
the other
folders there was a file named __init__.py, so I copied one
of those
init files to the utils folder. Now my IDLE shows this file
on the
Path.
I tried to import the TemplatedForm, but I still get this
error:
from Manager.utils import TemplatedForm

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in
<module>
    from Manager.utils import TemplatedForm
ImportError: cannot import name TemplatedForm

Why can't it import the thing?


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


Re: Forms and formating error messages
country flaguser name
United States
2007-06-26 11:26:12
I managed to solve this:

from Manager.utils.newforms import TemplatedForm


Thanks for the help!

Ana


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


Re: Forms and formating error messages
country flaguser name
United States
2007-06-27 04:26:01
Hi again,
This is getting very frustrating because I can't make this
work...
This is exactly what I wrote in the files:
[newforms.py]
from django import newforms as forms
from django.newforms.forms import BoundField
from django.template import Context, loader

class TemplatedForm(forms.Form):
    def cc(self):
        "Helper function for fieldsting fields data
from form."
        bound_fields = [BoundField(self, field, name) for
name, field

                        in self.fields.items()]
        c = Context(dict(form = self, bound_fields =
bound_fields))
        t = loader.get_template('newforms/form.html')
        return t.render(c)

    def __str__(self):
        return self.output_via_template()

[form.html]
{% for field in bound_fields %}
{% include "newforms/field.html" %}
{% endfor %}

[field.html]
<tr{% if field.errors %} class="errors" {%
endif%}>
  <th>
    <label for="id_{{ field.name }}">{{
field.label }}{% if
field.field.required %}<span
class="required">*</span>{% endif
%}:</
label>
  </th>
  <td>
    {}
    {% if field.errors %}{{ field.errors }}{% endif %}
    {% if field.help_text %}
    <p class="helptext">({{ field.help_text
}})</p>
    {% endif %}
  </td>
</tr>

This probably has to be modified to do what I want it to do
(present
the error text as simple text and not as <ul>) but I
have no idea
where I have to change this to do what I want it to do. I
know that
the changes have to be made on the function
output_via_template(), but
I just don't know what am I supposed to do there. :(

Ana


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


Re: Forms and formating error messages
country flaguser name
Australia
2007-06-27 04:41:21
On Wed, 2007-06-27 at 02:26 -0700, AnaReis wrote:
> Hi again,
> This is getting very frustrating because I can't make
this work...
> This is exactly what I wrote in the files:

In future, please remember that when you send a message like
this, a lot
of us are reading it through an e-mail interface, so include
the
necessary context to help us understand the problem. I ended
up having
to go back and read all your earlier messages in the thread
to work out
what the problem is that you were having. People, including
me, may not
always feel like spending that much time to answer a
question, so please
help us to help you.

If I understand correctly, you want to have control over
error message
presentaiton.

[... snip...]
> [field.html]
> <tr{% if field.errors %} class="errors" {%
endif%}>
>   <th>
>     <label for="id_{{ field.name }}">{{
field.label }}{% if
> field.field.required %}<span
class="required">*</span>{% endif
%}:</
> label>
>   </th>
>   <td>
>     {}
>     {% if field.errors %}{{ field.errors }}{% endif %}

So this is the problem. If you look at field classes (in
newforms.fields), you can see that the errors attribute is
a
newforms.util.ErrorList class and the __str__ method for
ErrorList is
the as_ul() method -- displaying results as an unordered
list.

If you want to control the presentation, you will need to
iterate over
field.errors and write out the results one by one. Or you
could write a
filter that applies to field.error and does this for you.
Remember that
ErrorList is a subclass of Python's standard lists, so
something like
(untested):

        {% if field.errors %}
           {% for error in field.errors %}
              {}<br />
           {% endfor %}
        {% endif %}
        
This particular example would just dump the strings with br
tags between
them, but you can obviously do whatever you want there.

Some experimentation will be required, but since you have
full access to
the raw error strings, anything should be possible.

Regards,
Malcolm

-- 
Works better when plugged in. 
http://www.pointy-s
tick.com/blog/


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


Re: Forms and formating error messages
country flaguser name
United States
2007-06-28 04:06:22


On Jun 27, 9:41 am, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Wed, 2007-06-27 at 02:26 -0700, AnaReis wrote:
> > Hi again,
> > This is getting very frustrating because I can't
make this work...
> > This is exactly what I wrote in the files:
>
> In future, please remember that when you send a message
like this, a lot
> of us are reading it through an e-mail interface, so
include the
> necessary context to help us understand the problem. I
ended up having
> to go back and read all your earlier messages in the
thread to work out
> what the problem is that you were having. People,
including me, may not
> always feel like spending that much time to answer a
question, so please
> help us to help you.
>
> If I understand correctly, you want to have control
over error message
> presentaiton.
>
> [... snip...]
>
> > [field.html]
> > <tr{% if field.errors %}
class="errors" {% endif%}>
> >   <th>
> >     <label for="id_{{ field.name
}}">{{ field.label }}{% if
> > field.field.required %}<span
class="required">*</span>{% endif
%}:</
> > label>
> >   </th>
> >   <td>
> >     {}
> >     {% if field.errors %}{{ field.errors }}{%
endif %}
>
> So this is the problem. If you look at field classes
(in
> newforms.fields), you can see that the errors attribute
is a
> newforms.util.ErrorList class and the __str__ method
for ErrorList is
> the as_ul() method -- displaying results as an
unordered list.
>
> If you want to control the presentation, you will need
to iterate over
> field.errors and write out the results one by one. Or
you could write a
> filter that applies to field.error and does this for
you. Remember that
> ErrorList is a subclass of Python's standard lists, so
something like
> (untested):
>
>         {% if field.errors %}
>            {% for error in field.errors %}
>               {}<br />
>            {% endfor %}
>         {% endif %}
>
> This particular example would just dump the strings
with br tags between
> them, but you can obviously do whatever you want
there.
>
> Some experimentation will be required, but since you
have full access to
> the raw error strings, anything should be possible.
>
> Regards,
> Malcolm
>
> --
> Works better when plugged in.http://www.pointy-s
tick.com/blog/
Hi,
Sorry for that, I didn't know you received this by e-mail, I
just come
here kind of like a forum... Won't happen again.

Thanks for your help, I managed to work this out by using
the usual
forms and not that TemplatedForm I was using before.
In the template I just put:
{%if field.errors%}<td><span
class="highlight"><b>{% for error in
field.errors %}    {}{%endfor%}</b></span></td>{%endif%}
This just prints the error as simple text exactly where I
want it to
appear.
It was so simple after all... 
Thanks for your help and sorry for all the trouble.

Ana


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


[1-8]

about | contact  Other archives ( Real Estate discussion Medical topics )