Django:如何更改Django表单元素的HTML输出? [英] Django: How do I change the HTML output of a Django form element?
问题描述
我有一个出生日期选择三个选择下拉框。我的Django模板如下所示:
< label>出生日期< / label>
{{reg_form.date_of_birth}}
但是我需要更改每个元素的HTML 。例如,我需要单独包含 div
中的每个选择
元素。
我在代码中查找了 date_of_birth
,我发现这个:
date_of_birth = forms.DateField(widget = SelectDateWidget(years = range(1920,2010)))
我可以修改这个HTML输出吗?
也许不是那么干净,我用jQuery解决了这个问题,如下所示: / p>
我针对HTML中由Django生成的三个选择元素,然后使用 .wrap()
函数将它们包装到 div
元素中。
I have a date of birth selection of three select drop down boxes. My Django template looks like this:
<label>Date of birth</label>
{{ reg_form.date_of_birth }}
But I need to change the HTML of each element. For example I need to wrap each select
element in a div
individually.
I had a look for date_of_birth
in the code and I found this:
date_of_birth = forms.DateField(widget=SelectDateWidget(years=range(1920, 2010)))
Is there a template somewhere that I can modify the HTML output of this?
Perhaps not so cleanly, I solved this problem using jQuery as follows.
I targeted each of the three select elements that were generated by Django in the HTML, and then used the .wrap()
function to wrap them in div
elements.
这篇关于Django:如何更改Django表单元素的HTML输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!