Django - Changing inline formset textInput size attribute

纵然是瞬间 提交于 2019-12-24 03:41:14

问题


I have an inline formset that only has three fields:

class Estimate_Product_Details(models.Model):
    proposalID = models.ForeignKey(Estimate_Construction, verbose_name='Proposal ID')
    CID = models.ForeignKey(Product, verbose_name = 'CID')
    qty = models.DecimalField(max_digits = 7, decimal_places = 2, verbose_name = 'Quantity')

    def __unicode__(self):
        return u'%s -- %s' % (self.proposalID, self.CID)

I then create a form from that model:

class Product_Form(ModelForm):
    class Meta:
        model = Estimate_Product_Details
        fields = ('CID', 'qty')
        widgets = {
            'qty' : forms.TextInput(attrs={'size':30})
            }

My goal is to have the qty input field really small (I have 30 there for testing). However, when I render this form via an inline formset, the attribute is not being set at all. Here's the creation of the formset in my view:

    pFormSet = inlineformset_factory(Estimate_Construction, Estimate_Product_Details, form = Product_Form)

Where am I going wrong? Why doesn't the qty field change in size?


回答1:


EDIT: It should probably just set a class to the input field and have the CSS assign the width. That way you could pass it to separate templates and such for different clients.

Perhaps instead of using .attrs['size'] = 30 use .attrs['class'] = 'some_class' then define the class in your HTML template and handle the different sizes there.

This should work:

class Product_Form(ModelForm):
    def __init__(self, *args, **kwargs):
        super(Product_Form, self).__init__(*args, **kwargs)
        self.fields['qty'].widget.attrs['size'] = 30

    class Meta:
        model = Estimate_Product_Details
        fields = ('CID', 'qty')



回答2:


I also had problems with @Furbeenator's code. However, on checking the resulting HTML, I found the size attribute was being appended to the <input> tag correctly.

The problem: I was using a third-party template which was overriding the <input> width with its own CSS definition.

The solution is to pass the width as a style attribute rather than size:

class UserProfileForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        self.fields['company'].widget.attrs['style'] = "width:500px"

This modification also works for @Furbeenator's View modification. I did not try the JQuery variant (although the template uses JQuery, I'm not at the moment).

The only downside is that the field width is now defined in terms of CSS units such as pixels or em/ex. I'm going to switch to ex, but it would be nice if CSS had an "average character width" unit so that you can define boxes like this to "hold 20 chars",etc.




回答3:


It is also possible to add a css class name for each input of form. To do it you should add widget attr to your definition of form input. Like bellow:

field_name = forms.CharField(label='label', max_length=100, widget=forms.TextInput(attrs={'class':'input-control'}))


来源:https://stackoverflow.com/questions/13612408/django-changing-inline-formset-textinput-size-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!