This seems like it should be obvious, but the solution is eluding me. Normally I would just write a simple view function which would populate an appropriate form and pass it along to the view, but the solution feels so close ..
I have a form. I want to instantiate this form using an object_id
that I've captured in the url, then send it through to my template using the extra_context
parameter.
I have something like this:
class AddProductForm(forms.Form):
product = forms.IntegerField()
quantity = forms.IntegerField()
and this:
url(r'^products/(?P<object_id>\d+)/$',
'django.views.generic.list_detail.object_detail',
{'queryset': Product.objects.all(),
'extra_context': {'form': AddProductForm({'product': <what?>, 'quantity': 1})},
name='product_detail'),
Is there a way to replace <what?>
above with the captured value of object_id
? (Maybe a clever callable passed in extra_context
could make the form for me?)