ModelForm to html?

28 views
Skip to first unread message

Max B

unread,
Sep 21, 2012, 6:25:42 AM9/21/12
to db...@googlegroups.com
Hey all,
I was wondering is anyone had seen any code that takes a ModelForm
object and can product a generic div structure in html? It would make
styling everything in CSS much easier. I attempted this once a while
ago and got lost in model introspection, I might have another go
taking a different approach now I've learnt a bit more.

Thanks, Max

Stuart Cox

unread,
Sep 22, 2012, 8:57:42 AM9/22/12
to db...@googlegroups.com
Hi Max,

There are already form.as_p(), form.as_ul() and form.as_table() quick methods which will give various HTML representations of your form, but no - there isn't a "standard" div-based equivalent of these. There have been a number of requests to include a form.as_div() method in the core, but these have been rejected because the core devs would rather encourage all HTML to be defined in templates.

There are a few snippets of people defining custom as_div() methods - e.g. here - but that's one person's idea of how a form should be marked up. There isn't a one-size-fits-all, so I'd say you're probably better off defining the markup yourself in a template and populating it with the form's data/fields/values in the right places.

I wouldn't really recommend using the existing as_X() methods for anything other than debugging / rough prototyping, i.e. before you write the bulk of your CSS.


Stuart



--
You received this message because you are subscribed to the Google Groups "Django Bath and Bristol Users Group" group.
To post to this group, send email to db...@googlegroups.com.
To unsubscribe from this group, send email to dbug+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/dbug?hl=en.


Cameron Maske

unread,
Sep 23, 2012, 10:48:51 AM9/23/12
to db...@googlegroups.com
Hi Max, 

Maybe django crispy forms would work for you? You define the forms template/layout structure in the form class. You can easily wrap fields in divs.  I have found it a lot more manageable then custom templates.  
--
Developer at Closed Loop Labs
I'm also on Twitter.

Max B

unread,
Sep 24, 2012, 7:39:13 AM9/24/12
to db...@googlegroups.com
django crispy forms look like exactly what I'm looking for, I did
write a custom as_div function but apparently the django developers
don't want to include any more html in the code base.

Thanks guys.
Reply all
Reply to author
Forward
0 new messages