rendering modelForms

136 views
Skip to first unread message

Marilena Papageorgiou

unread,
Jul 9, 2012, 7:06:32 AM7/9/12
to django...@googlegroups.com
Hi!
In my django app I'm using modelForms and not simple django forms so in order to render the form I just do  {{ form.as_table }} an the template and everything is being displayed. 
That's simple, but obviously any html code is being skipped, so I wonder how will I further customize my form (css stuff etc)+(I'd like to use twitter bootstrap) 
Is there any way? I also want to upload a file, but how am I going to add the required header  <form method="post" action='./'{% if form.is_multipart %} enctype='multipart/form-data'{% endif %}> since my form is being automatically rendered only by {{ form.as_table }}? It confuses me.. can anyone help?

Thanks in advance



Daniel Roseman

unread,
Jul 9, 2012, 9:00:17 AM7/9/12
to django...@googlegroups.com
You *always* need to supply the wrapping <form> ... </form> tags yourself. form.as_table doesn't output them.

However to answer the rest of your question, see the documentation:
where this is discussed in detail.
--
DR.

mapapage

unread,
Jul 10, 2012, 7:06:45 AM7/10/12
to django...@googlegroups.com
I already read that, but my question is how can I css-style only some of my fields while the rest continue to be rendered by form.as_table. Is there such an option?
I want that because with modelForms I have a direct mapping of my forms to my models and if I customize them manually I loose for example some dropdown lists that load data to a field that is a foreign key, what's more I loose the error messages (this field is required etc) that with form.as_table appear automatically. 
Do you have a hint maybe?

kenneth gonsalves

unread,
Jul 10, 2012, 7:40:19 AM7/10/12
to django...@googlegroups.com
On Tue, 2012-07-10 at 04:06 -0700, mapapage wrote:
> I already read that, but my question is how can I css-style only some
> of my fields while the rest continue to be rendered by form.as_table.
> Is there such an option?

that I do not think you can do. What you have to do is to drill down
into the form and massage the elements. Please look at this code to get
a general idea. The code does not touch the css, but you can add css as
you please:

https://bitbucket.org/lawgon/fossconf/src/71c974a48f25/templates/web/edittalk.html



--
regards
Kenneth Gonsalves

Jirka Vejrazka

unread,
Jul 10, 2012, 9:05:19 AM7/10/12
to django...@googlegroups.com
Hi Marilena,


I'm also using Twitter Bootstrap and over time migrated to this
template snipped that I'm including in my templates at the place where
you put {{ form.as_table }}

http://dpaste.com/hold/768995/

If you find it useful, great :) I'm not a web developer by nature
so there may be a better way to do this - just be warned :)

HTH

Jirka

Tomas Neme

unread,
Jul 10, 2012, 12:07:16 PM7/10/12
to django...@googlegroups.com
There's a way, but you'll need to touch the form on the python side
quite a lot, changing the default widgets adding them the css classes
you want.

You can take a look at https://github.com/earle/django-bootstrap and
https://github.com/dyve/django-bootstrap-toolkit/ they provide some
shortcuts for this


--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.
Reply all
Reply to author
Forward
0 new messages