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?

1 Answer 1

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"})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.