views:

216

answers:

1

I would like to change an external apps form fields to use in a particular template of my application.

I would like to give attribute values for the fields as shown below.

body = forms.CharField(widget=forms.Textarea(attrs={'class':'textarea', 'rows':'', 'cols':"", 'onclick':"this.style.height='250px';"}))
title = forms.CharField(widget=forms.TextInput(attrs={'class':'text'}))
tags = forms.CharField(widget=forms.TextInput(attrs={'class':'text'}))

I know, I can assign these attributes in the external apps forms.py; but I need to assign these attributes for a specific template in order not to affect other parts of the application.

+1  A: 

If you want those attributes only on a single template, you have two choices:

  1. Lay out the form manually in that template. The disadvantage is that it's not very DRY, and you'll have more work to do updating that template if the reusable app's form ever changes in future upgrades.

  2. Patch those attrs onto the Field objects after the form instance is created in the view that renders that template. If the view is also part of the reusable app, this may result in some DRY violations too if you have to reimplement that view yourself.

Carl Meyer
I'd definitely hard-code the form here. Keeping the new view in sync with arbitrary model/urlpattern changes is going to be more work than keeping display logic in sync with changes to that particular form.
ozan