问题
This has me stumped. (But I'm new at this.) The generic view object_detail is called with a queryset and the id field of interest. When object_detail then renders the template it is done with the template_object_name. When I write the template I use the template_object_name and can display all the fields. OK, that's nice, but I have a form I made with ModelForm.
So how do I get the form to show up? I've looked around for this. I tried passing the form in extra_context so that the form could accept the data in the object that is used to render the template, but no go.
The workaround is just writing a view. The problem with object_detail is that the SQL that pull out the row by specified id is done inside object_detail and then the object goes to the view. If I'm interested in using the form that I created with ModelForm then I want to set a form=mymodelForm(object) and pass form to the template for rendering. Another way to ask my question is where is the hook in object_detail that specifies what kind of object is specified?
回答1:
The answer is available on Using class based generic view DetailView with a ModelForm reveals a bug - how to proceed?
Thanks to everyone. The comments to the related questions allowed me to figure this out. Couldn't have done this without you!
来源:https://stackoverflow.com/questions/6522808/using-form-in-object-detail