I only want to create
forms.ChoiceField(choices=CHOICES)
field to create a simple select html item. Because this is all I want, I don't really want to create a new form class etc. So is there a way to render only one form field in django templates without creating/passing in a form?
for example:
def index(request, template_name="main/index.html"):
select = forms.ChoiceField(choices=CHOICES)
return render_to_response(template_name, locals(), context_instance=RequestContext(request))
is there a way I can do {{ select }} in 开发者_如何学运维the django template or something like that to create a select item?
Thanks in advance!
If you don't need an entire Django form class, I would recommend just passing the list of choices to the template and iterating over it to create the <option />
tags for the <select>
. That's a lot easier than trying to render a form widget at the template level, imo.
only one choices field? why not just write all HTML code into your template and only send the CHOICES dict to your template by using templatetags to handle it.
精彩评论