Reputation: 131
I have an app that uses a form that a customer will fill out and submit lets call it formA. Now I also have the same form being used by the staff with some additional fields being rendered. In my forms.py I am forced to include those added fields to the form even though I dont want them to be rendered from the customers point of view or else I cant use the same form for the staff.
Currently I have been including logic in the template to ignore these fields that I dont want by using the {% ifnotequal %}
tag with field.label
in a for loop iteration over the fields but I feel as it is becoming too cumbersome and confusing to read.
I know another option would have been to create a separate form but then I feel that would violate DRY.
Is there a preferred way of reusing forms in this case or a better way of going about this?
Upvotes: 1
Views: 34
Reputation: 308999
You can create different forms for the customer and staff views, and then use inheritance to avoid repetition.
class BaseForm(forms.Form):
field1 = forms.CharField()
class StaffForm(BaseForm):
staff_field = forms.CharField()
class CustomerForm(BaseForm):
customer_field = forms.CharField()
I would avoid using {% ifnotequal %}
in the template to decide whether or not to display fields -- logic like this does not belong in the template.
Upvotes: 1