views:

219

answers:

2

Hi,

with Django forms you can specify widget:

class LoginForm(forms.Form):
    email = forms.CharField(
     max_length = 30,
     widget = forms.TextInput(
      attrs = {'class':'text required email', 'id':'email'}))
    password = forms.CharField(
     max_length = 20,
     widget = forms.PasswordInput(
      attrs = {'class':'text required', 'id':'password', 'minlength':'4'}))

is it possible to do that in models?

class Order(models.Model):
    name =    models.CharField(max_length = 256)
    phone =    models.CharField(max_length = 256)
    email =    models.CharField(max_length = 256)
    start =    models.CharField(max_length = 256)
    destination =   models.CharField(max_length = 256)
    size =    models.CharField(max_length = 256)

I'd love to use this (specify CSS class) when creating form for model like:

class OrderForm(ModelForm):
    class Meta:
     model = Order

Thanks in advance, Etam.

+3  A: 

You can use the following method to specify a css class:

class IssueForm(forms.ModelForm):
    """Form for adding issues."""

    def __init__(self, *args, **kwargs):
        super(IssueForm, self).__init__(*args, **kwargs)
        self.fields['description'].widget.attrs['class'] = "span-3 last"

Let me know if you need more — Good luck!

lemonad
+2  A: 

No, but you can redefine widget by overriding the fields in your modelform.

Another approach would be to implement __init__ method and change the widget or attr in the existing fields.

Kugel