Home > Software engineering >  Adding button to a field in a form to add data from another form Django
Adding button to a field in a form to add data from another form Django

Time:06-18

So, I have this html file with a model form and I would like to add a button ' ' to just one field of the form to to open a modal with another smaller form to fill out and just take information from one column. I already have the way to open the modal and the second form, in the database I am guessing I will need a foreign key, but I am still trying to figure out how to implement the button right next to the field. So far, I have this in my forms.py file:

class contractsform(forms.ModelForm):
    class Meta:
        model = Contratos
        fields = '__all__'

        widgets = {
            'name': forms.TextInput(attrs ={'class': 'form-control'}),
            'contractee': forms.TextInput(attrs={'class': 'form-control'}),
            'contractor': forms.TextInput(attrs={'class': 'form-control'}),
            'start': forms.NumberInput(attrs={'class': 'form-control', 'type': 'date'}),
            'end': forms.NumberInput(attrs={'class': 'form-control', 'type': 'date'}),
            'cost': forms.NumberInput(attrs={'class': 'form-control'}),
            'type': forms.Select(attrs={'class': 'form-select'}),
            'department': forms.Select(attrs={'class': 'form-select'}),
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'product': forms.TextInput(attrs={'class': 'form-control'}),
            'attached_file': forms.FileInput(attrs={'class': 'form-control'}),
            'notification': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
            }

I added attrs just to make the fields pretty. I would like to add it to the contractor field, with bootstrap 5, but I am new to Django and the manual confused me a little bit.

CodePudding user response:

Does form manual rendering fit your need Django Working with forms? You can add HTML of the " " button next to the rendered field.
However, you may have to do extra work with the POST request in view to save the model correctly.

  • Related