Skip to content Skip to sidebar Skip to footer

Passing Custom Form Values To Views

I'm trying to pass values from my custom form to my views.py . However, I can't seem to pass a value for each one of my multiselect options. When rendering there is only 1 charfiel

Solution 1:

You need to write a loop to generate required amount of fields. Example:

outcome_qs = Outcome.objects.all()
self.fields['relevantoutcome'] = forms.ModelMultipleChoiceField(queryset=outcome_qs, required=True, widget=forms.CheckboxSelectMultiple)
for outcome in outcome_qs:
    # Use Outcome primary key to easily match two fields in your view.
    self.fields['outcomevalue_%s' % outcome.pk] = forms.CharField(widget=forms.TextInput(attrs={'size':'30'}) 

Post a Comment for "Passing Custom Form Values To Views"