ModelForms

27 views
Skip to first unread message

hack

unread,
Mar 22, 2012, 10:46:25 PM3/22/12
to django...@googlegroups.com
I think I shot myself in the foot using ModelForms to generate all of my html forms.  Is there any way to use a stylesheet when your forms are generated from ModelForms?

I've tried everything and cannot get it to work.  I've tried directly importing the css files, I've tried loading them from STATIC, and I've tried setting css in Meta, but nothing seems to work.  All I have to do is add some color to my tables and align the text to the left of the cells.  Thanks.

Mario Gudelj

unread,
Mar 23, 2012, 7:10:42 AM3/23/12
to django...@googlegroups.com
It doesn't matter how you generate your form, your CSS should be applied from inside the template. You need to add a CSS file link to a template to which you're passing the form to inside your context from within a view, or you can embed it into your template using <style> tags.

On 23 March 2012 09:46, hack <scott...@gmail.com> wrote:
I think I shot myself in the foot using ModelForms to generate all of my html forms.  Is there any way to use a stylesheet when your forms are generated from ModelForms?

I've tried everything and cannot get it to work.  I've tried directly importing the css files, I've tried loading them from STATIC, and I've tried setting css in Meta, but nothing seems to work.  All I have to do is add some color to my tables and align the text to the left of the cells.  Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/d_3ry3L6DuEJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Tim Ney

unread,
Mar 23, 2012, 4:25:27 PM3/23/12
to django...@googlegroups.com
I think the best thing for you do is to create the css outside the template. Like this - create the css file, separately, then reference the file in the template.
 
Or, better yet, add the css you need for your form in you main css file that is referenced in your base template and just inherit it by
using a tag {{ extends "base.html""}, then you will have a uniform look throughout the site and you won't have to worry about but once.

Kurtis Mullins

unread,
Mar 23, 2012, 4:34:59 PM3/23/12
to django...@googlegroups.com
If you're worried about stylizing specific fields, it'll be setup as a table, ul, or paragraph based upon how you "render the form" (paragraph by default I think). The individual fields are named with the following convention (if I recall correctly) <input id="id_<field_name>" ... />. So for a field named "password" in a paragraph rendered form, it'll end up sort of like this:

<p><label for="id_password">password</label><input type="password" name="password" id="id_password" /></p>

Of course I don't use these pre-rendered forms because mine typically need too much customization. So what we do is the following:

<form action="" method="post">{% csrf_token %}
...
<input name="password" type="password" value="{{ form.password.value }}" />
<input type="submit" value="Click Me!" />
...
</form>

and you can easily add in your own stylizing CSS and elements as needed.

Tim Ney

unread,
Mar 23, 2012, 5:01:56 PM3/23/12
to django...@googlegroups.com
Not to gild the lilly, but, you could also do this, and it would work: either as part of the css you are inheriting from the "base" or, separately, you could create a whole slew of named css styles in one file, then apply each file to the template tags, table tags you use to call the data into the template as you see fit, you could then modify the css rther easily to get the appearance you want. I don't know if this helps, or not, but all my static files, css, javascript are called up by a separate server, not apache, and if you don't have all the urls right, no static file, css renders.

Eryn Wells

unread,
Apr 27, 2012, 1:55:51 AM4/27/12
to django...@googlegroups.com
On Thu, Mar 22, 2012 at 3:46 PM, hack <scott...@gmail.com> wrote:
> I think I shot myself in the foot using ModelForms to generate all of my
> html forms.  Is there any way to use a stylesheet when your forms are
> generated from ModelForms?

You should be able to render the forms and plug the rendered HTML into
a template, which will let you do whatever you want in terms of
including style sheets and such...

How have you been trying to do it?

Eryn

bruno desthuilliers

unread,
Apr 27, 2012, 11:50:00 AM4/27/12
to Django users
On Mar 23, 12:46 am, hack <scottma...@gmail.com> wrote:
> I think I shot myself in the foot using ModelForms to generate all of my
> html forms.  Is there any way to use a stylesheet when your forms are
> generated from ModelForms?

Yes of course, why ?

> I've tried everything and cannot get it to work.  I've tried directly
> importing the css files, I've tried loading them from STATIC, and I've
> tried setting css in Meta, but nothing seems to work.

Your problem is with serving the css files, not with Django's form.
Reply all
Reply to author
Forward
0 new messages