views:

134

answers:

1

I have a Django form that is subclassed from the django-contact-form application. I want to supply an initial parameter to the form (which will vary depending on context).

This code returns the contact form fine, but obviously doesn't supply an initial parameter, so I need to extend it:

def contact_form(request):
 scraper_form = scraperContactForm
 return contact_views.contact_form(request=request, form_class=scraper_form)

This attempt to supply an initial parameter fails:

def contact_form(request):
 scraper_form = scraperContactForm(initial={'title' : 'hello world'})
 return contact_views.contact_form(request=request, form_class=scraper_form)
TypeError at /contact/
Keyword argument 'request' must be supplied

Hence, I have tried to supply a request argument, but weirdly, that fails by saying the form object is not callable:

def contact_form(request):
 scraper_form = scraperContactForm(request=request, initial={'title' : 'hello world'})
 # supply initial subject_dropdown field, if there's anything in the subject_type
 return contact_views.contact_form(request=request, form_class=scraper_form)
TypeError at /contact/
'scraperContactForm' object is not callable

And no matter how I try to supply the request parameter, I keep on getting 'scraperContactForm' object is not callable.

FYI, this is the code for my subclassed form:

class scraperContactForm(ContactForm):
  subject_dropdown = django.forms.ChoiceField(label="Subject type", choices=(('suggestion', 'Suggestion for improvement'), ('bug', 'Report a bug'), ('other', 'Other')))
  title = django.forms.CharField(widget=django.forms.TextInput(), label=u'Subject')
  recipient_list = [settings.FEEDBACK_EMAIL]

Please can anyone suggest what's going wrong?

A: 

You don't supply the full traceback in any of your examples. If you had, I suspect we would see that it's not your code that's giving the 'scraperContactForm' object is not callable error, but the subsequent call to the main contact_form view.

This is because that view is clearly expecting a form class, as indicated by the keyword parameters. However, you're already instantiating the form, by calling it in your first line, so you're actually passing a form instance, which isn't callable.

I would suggest that if you want to provide an initial value for a field, you do so in your subclassed form's definition:

class scraperContactForm(ContactForm):
    title = django.forms.CharField(initial='hello world')
Daniel Roseman
Thank you. However, I would like the 'initial' value to vary, so I do need to supply it in the views code somewhere. Is this actually possible?
AP257