开发者

Django form for model

开发者 https://www.devze.com 2022-12-10 09:07 出处:网络
with Django forms you can specify widget: class LoginForm(forms.Form): email = forms.CharField( max_length = 30,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消