Django form for model
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,
widge开发者_如何学运维t = 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.
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!
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.
精彩评论