Home > Net >  How can I pass True into form if one field is selected and False if another
How can I pass True into form if one field is selected and False if another

Time:02-24

I have select field in my html

<select  name="is_recurrent" id="payment">
    <option value="False"  selected>One-time fee</option>
    <option value="True" >Subscription</option>
</select>

I have model Donate in models.py

class Donate(BaseModel):
    is_recurrent = models.BooleanField(default=False)
    ...

I have form in forms.py

class DonateForm(forms.Form):
    is_recurrent = fields.BooleanField(???)
    ...

How can I pass True into form if subscription is selected and false if one-time fee is selected

CodePudding user response:

When you instantiate your form in the view, you pass in the POST data:

form = DonateForm(data=request.POST)

That will auto-populate the form with the information that the user selected in the form prior to form submission.

CodePudding user response:

If you're using Django-forms, it's really easy to do so, you don't even need to build the form! From your HTML code I can see you are trying to do so, creating an HTML form, I recommend you going through Django-forms information page (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms) since you don't really need to create all of the input boxes, the Django Forms takes care of that for you, you just need to pass the variables you want to edit using the form.

  • Related