How to stop Django from adding extra html elements to rendered widgets
I have a Django radio button group that renders to HTML as follows:
<ul>
<li><label for="id_package_id_0"><input type="radio" id="id_package_id_0" value="1" name="package_id" /> Test 256</label></li>
<li><label for="id_package_id_1"><input type="radio" id="id_package_id_1" value="2" name="package_id" /> Test 384</label></li>
<li><label for="id_package_id_2"><input type="radio" id="id_package_id_2" value="3" name="package_id" /> Test 512</label></li>
<li><label for="id_package_id_3"><input type="radio" id="id_package_id_3" value="4" name="package_id" /> Test 768</label></li>
<li><label for="id_package_id_4"><input type="radio" id="id_package_id_4" value="5" name="package_id" /> Test 1024</label></li>
</ul>
I need it to render wit开发者_开发百科hout being a list. I am a aware of form.as_p, form.as_table, and form.as_ul. They will not help me as they continue to add extra HTML tags. As well, I am not using the form object in it's absolute entirety, just for validation. I am doing a custom template for the form already, but wish to continue to the radio widget.
Check out Django 1.2 (released today), which includes build in Model Validation.
Then just write out your forms by hand :)
To render just that one field of your form, you need to output your field in the template with something like this:
{{ form.my_radiofield }}
That will output just the one widget without any extra table, p or ul markup. You can read more about that here.
I guess you will have to subclass the necessary widgets and overwrite their render()
-method!
精彩评论