How to change the size of input field from Django

7,095 views
Skip to first unread message

Pythoni

unread,
May 4, 2007, 2:13:45 AM5/4/07
to Django users
One of the field in my model is defined
Description=meta.TextField()

When I use
{{form.Description}}
in my input form( template)
the field is not as large as I would need.
When I check HTML I can see

<textarea id="id_Description" class="vLargeTextField required"
name="Description" rows="10" cols="40"></textarea>

But I would need at least rows="20" cols="70".
Is there a way how to change the size from Django directly and not by
editing HTML code?
Thank you
L.B.

Bruno Tikami

unread,
May 4, 2007, 9:25:26 AM5/4/07
to django...@googlegroups.com
You might also want to take a look at contrib/admin/views/template.py 's class TemplateValidator

On 5/4/07, Bruno Tikami < bruno...@gmail.com> wrote:
Hi Pythoni!

have you tried to take a look at  oldforms/__init__.py file? There, look for the LargeTextField class and it's render function. I understand it's not exactily what you wanted, but it's a point to start ;)


good luck!


Tkm

Bruno Tikami

unread,
May 4, 2007, 9:23:22 AM5/4/07
to django...@googlegroups.com
Hi Pythoni!

have you tried to take a look at  oldforms/__init__.py file? There, look for the LargeTextField class and it's render function. I understand it's not exactily what you wanted, but it's a point to start ;)


good luck!


Tkm


On 5/4/07, Pythoni <pyt...@hope.cz> wrote:

Russell Keith-Magee

unread,
May 4, 2007, 9:16:54 PM5/4/07
to django...@googlegroups.com
On 5/4/07, Pythoni <pyt...@hope.cz> wrote:
>

Using newforms, yes:

Pass in an 'attrs' argument to the Textarea widget. e.g.,

class MyForm(forms.Form):
text = fields.TextField(widget=widgets.Textarea(attrs={'cols':'70',
'rows':'20'}))

This defines a text field that will use a Textarea widget to render;
by default, Textarea uses 1 40x10 area, but this can be overridden.

if you want to get really fancy, you can define your own subclass of
Textarea that hard codes your preferred sizes, and then request that
the TextField you the new widget :

class MyTextarea(widgets.Textarea):
def __init__(self, attrs={}):
attrs.update({'cols':'70', 'rows':'20'})
super(MyTextarea, self).__init__(attrs)

class MyForm(forms.Form):
text = fields.TextField(widget=widgets.MyTextarea)

(Note - I haven't run this code so it might have a syntax error or
two, but it should be enough to give you an idea and get you on the
way).

Yours,
Russ Magee %-)

Alessandro Ronchi

unread,
May 4, 2007, 10:27:58 PM5/4/07
to django...@googlegroups.com
Alle venerdì 04 maggio 2007, Pythoni ha scritto:
> But I would need at least rows="20" cols="70".
> Is there a way how to change the size from Django directly and not by
> editing HTML code?
> Thank you
> L.B.

with newforms is very simple.
you create the form from the model, like
ContactForm = forms.models.form_for_model(Contact)

and then you can change the atttribute:
ContactForm.base_fields ["name"].widget.attrs ["size"] = "50"

or

ContactForm.base_fields ["message"].widget.attrs ["rows"] = "10"
ContactForm.base_fields ["message"].widget.attrs ["cols"] = "50"

please tell me if it's the solution for your problem.
--
Alessandro Ronchi
Skype: aronchi - Wengo: aleronchi
http://www.alessandroronchi.net - Il mio sito personale
http://www.soasi.com - Sviluppo Software e Sistemi Open Source

Pythoni

unread,
May 5, 2007, 2:07:17 AM5/5/07
to Django users

On May 5, 4:27 am, Alessandro Ronchi <alessandro.ron...@gmail.com>
wrote:


> Alle venerdì 04 maggio 2007, Pythoni ha scritto:
>
> > But I would need at least rows="20" cols="70".
> > Is there a way how to change the size from Django directly and not by
> > editing HTML code?
> > Thank you
> > L.B.
>
> with newforms is very simple.
> you create the form from the model, like
> ContactForm = forms.models.form_for_model(Contact)
>
> and then you can change the atttribute:
> ContactForm.base_fields ["name"].widget.attrs ["size"] = "50"
>
> or
>
> ContactForm.base_fields ["message"].widget.attrs ["rows"] = "10"
> ContactForm.base_fields ["message"].widget.attrs ["cols"] = "50"
>
> please tell me if it's the solution for your problem.
> --
> Alessandro Ronchi

Thank you ALL who replied to help me solve the problem.
Unfortunately my program still uses 0.91 Django version so I do not
think I can use newforms.
Would be there any solution with oldforms? Or would you recommend to
add, to the form, HTML code of text area like this:

<textarea id="id_Description" class="vLargeTextField required"

name="Description" rows="20" cols="70"></textarea>


?
(That code is generated by Django)

But then I do not know how easy it would be to fill in the input text
area with the data to be edited.
Any idea would be appreciated
L.


jordi.f

unread,
May 8, 2007, 2:27:12 PM5/8/07
to Django users
Hi L.B.,

I copy to 'myproject/templates/admin/base_site.html' the template from
the Django source tree: 'django/contrib/admin/templates/admin/
base_site.html', so I can overwrite it without patching Django.

Then I add something like

{% block extrastyle %}
<style type="text/css">
textarea#id_Description { width: 60em; height: 35em; }
</style>
{% endblock %}

That's enough for me, and works with oldforms.

Richard House

unread,
Aug 15, 2007, 12:42:09 PM8/15/07
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages