开发者

Displaying a single form field in django templates?

开发者 https://www.devze.com 2023-03-22 04:22 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号