0

I am trying to achieve submitting a form with input fields being outside of the form (see here). Therefore I want to add the form="" attribute to my inputfields, as described here:

class TestForm(forms.Form):
    class Meta:
        model = Product
        fields = ["number"]
        
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["number"] = forms.IntegerField(required = True)
        self.fields["number"].widget.attrs.update({"class": "form-control w-50"}) ## works
        self.fields["number"].widget.attrs.update({"form": "testformid"}) ## does not work

in the template the inputfield renders as:

<input type="number" name="number" value="8" class="form-control w-50" required="" id="id_number">

how can I add the form="..." correctly?

xtlc
  • 1,070
  • 1
  • 15
  • 41

1 Answers1

1

try using data attributes and using one single update call

class TestForm(forms.Form):
    class Meta:
        model = Product
        fields = ["number"]
        
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["number"] = forms.IntegerField(required = True)
        self.fields["number"].widget.attrs.update({"class": "form-control w-50","data-form": "testformid"})