Reputation: 4504
I am trying to write a simple view that renders a single field that I create outside of any form:
field = ModelChoiceField(queryset=MyModel.objects.all(),empty_label=None)
template = Template("<div>here is my field: {{field}}</div>")
context = Context({"field" : field})
return HttpResponse(template.render(context));
(This will form the content of a JQuery dialog; I access this view via AJAX.) However, instead of rendering the ModelChoiceField, it just displays the following sort of text:
here is my field <django.forms.models.ModelChoiceField object at 0x96576cc>
How can I get this to render normally?
Upvotes: 2
Views: 527
Reputation: 31951
Because the field won't be rendered outside the form. So, use the form, it's clean, easy to understand and doesn't have any overhead.
If you want to know, why exactly your form isn't rendered, you can look at the source of the field. It doesn't have __unicode__
method. Only BoundField
has it. So you need to use form to make your field bound.
May be there is another way, but using the form is the most starightforward and clean.
Upvotes: 3