对齐单选按钮水平在Django形式 [英] Align radio buttons horizontally in django forms

查看:1016
本文介绍了对齐单选按钮水平在Django形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

欲水平对齐的单选按钮。默认情况下Django的形式显示在垂直格式。

I want to align the radio buttons horizontally. By default django forms displays in vertical format.

feature_type  = forms.TypedChoiceField(choices = formfields.FeatureType, widget = forms.RadioSelect)

有没有办法,我们可以通过单选按钮对齐的任何特殊参数?

Is there any special parameter that we can pass for radio buttons alignment?

在此先感谢

推荐答案

多数民众赞成在 RadioField 的行为。如果你想要呈现的水平,创建水平的渲染器,喜欢的东西如下:

Thats the behavior of the RadioField. If you want it rendered horizontally, create a horizontal renderer, like something as follows:

from django.utils.safestring import mark_safe

class HorizontalRadioRenderer(forms.RadioSelect.renderer):
  def render(self):
    return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))


class ApprovalForm(forms.Form):
    approval = forms.ChoiceField(choices=APPROVAL_CHOICES,
                 initial=0,
                 widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),
                                 )

这篇关于对齐单选按钮水平在Django形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆