gpt4 book ai didi

django - 在 Django 中自定义单选按钮

转载 作者:行者123 更新时间:2023-12-04 17:36:36 26 4
gpt4 key购买 nike

模板代码是

{{ form.incident_live }}

表格.py
INCIDENT_LIVE = (
('0', 'Live'),
('1', 'Test'),
)
class IncidentForm(forms.ModelForm):
incident_live = forms.ChoiceField(widget=forms.RadioSelect(),choices=INCIDENT_LIVE)

上面的代码给了我垂直顺序选择的单选按钮,但我希​​望它是水平的,即等效的 html 是 <input type="radio" name="status" />Live <input type="radio" name="status" checked="checked"/> Test .

提前致谢

最佳答案

听起来像是自定义小部件渲染器的工作:

from django.utils.safestring import mark_safe

class HorizRadioRenderer(forms.RadioSelect.renderer):
""" this overrides widget method to put radio buttons horizontally
instead of vertically.
"""
def render(self):
"""Outputs radios"""
return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))

class IncidentForm(forms.ModelForm):
incident_live = forms.ChoiceField(widget=forms.RadioSelect(renderer=HorizRadioRenderer),choices=INCIDENT_LIVE)

取自 https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons

关于django - 在 Django 中自定义单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16773579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com