DSblizzard
DSblizzard

Reputation: 4149

How to display ChoiceField in a template?

Excerpts from
forms.py:

class ContForm():
    KafChoices = [(kaf.id, kaf.name) for kaf in Kaf.objects.all()]
    kaf = forms.ChoiceField(choices = KafChoices, required = True)

    class Meta:
        model = Cont

views.py:

def index(request):
    return render_to_response('db3/index.html',
        {'form': ContForm()},
        context_instance=RequestContext(request))

index.html:

{{ form.kaf }}

I get following output:
django.forms.fields.ChoiceField object at 0x01A110D0
instead of usual field. What's wrong?

Upvotes: 1

Views: 835

Answers (1)

Because ContForm isn't inheriting from forms.Form?

Edit: looks like a modelform, in which case ContForm(forms.ModelForm), but either way if you don't inherit from the form classes all form.kaf is doing is pulling up this python object that doesn't know what to do with itself.

Upvotes: 2

Related Questions