在Django表单中,如何渲染单选按钮,使选择在页面上分开? [英] In a Django form, how do I render a radio button so that the choices are separated on the page?

查看:492
本文介绍了在Django表单中,如何渲染单选按钮,使选择在页面上分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有双选单选按钮元素的Django表单。我希望表单或多或少地呈现:

 
()我更喜欢啤酒
我参加的最后一场体育赛事是:[]
我最喜欢的NASCAR驱动程序是:[]

()我更喜欢葡萄酒
我参加的最后一个歌剧/播放是:[]
我最喜欢的作家是:[]

换句话说,我想分割两个单选按钮选项。我怎么做?使用默认的form.as_table渲染,这些选择被画在彼此旁边,我不想要。



(向NASCAR和歌剧爱好者致歉。 p>

解决方案

我做了一点挖掘,发现在这个类似的帖子,表示有点过时的在django-users中发布



借用一些代码在中的as_widget()方法forms.py 我创建了一个方法,我可以添加到我的表单,以检索RadioSelect小部件的呈现为HTML的选择。

  class MyForm(forms.F orm):
MY_CHOICES =(
('opt0','选项零'),
('opt1','选项一'),

myfield = forms.ChoiceField(widget = forms.RadioSelect,choices = MY_CHOICES)

def myfield_choices(self):

返回myfield的小部件的默认渲染器,可以使用到
呈现RadioSelect小部件的选择。

field = self ['myfield']
widget = field.field.widget

attrs = {}
auto_id = field。 auto_id
如果auto_id和'id'不在widget.attrs中:
attrs ['id'] = auto_id

name = field.html_name

return widget.get_renderer(name,field.value(),attrs = attrs)

然后在模板中您可以访问如下单独的单选按钮选项:

 < ul> 
< li>
{{myform.myfield_choices.0}}
我的自定义HTML随选择0
< / li>
< li>
{{myform.myfield_choices.1} }
与选择1不同的HTML
< / li>
< / ul>

  {%for myform.myfield_choices%} 
< div>
{{choice}}
< / div>
{%endfor%}

我很确定这是一个坏主意。随着django进化,它可能会停止在某一时刻的工作。它通过从as_widget()中复制代码来违反DRY。 (TBH,我没有时间充分了解as_widget中的代码)我仅将它用作临时黑客。也许有一个更好的方法涉及自定义模板标签。如果是,请让我知道。


I have a Django form with a two-choice radio button element. I want the form to render more or less like this:

( ) I prefer beer
     The last sporting event I attended was: [           ]
     My favorite NASCAR driver is:           [           ]

( ) I prefer wine
     The last opera/play I attended was:     [           ]
     My favorite author is:                  [           ]

In other words, I want to split up the two radio button choices. How do I do that? Using the default form.as_table rendering, the choices are drawn right next to each other which I don't want.

(Apologies to NASCAR and opera enthusiasts.)

解决方案

I did a little digging, and found a lead in the answer to this similar post that points to a somewhat outdated post in django-users.

Borrowing some code from the as_widget() method in forms.py I fashioned a method that I could add to my form in order to retrieve the RadioSelect widget's choices rendered as HTML.

class MyForm(forms.Form):
    MY_CHOICES = (
        ('opt0', 'Option zero'),
        ('opt1', 'Option one'),
    )
    myfield = forms.ChoiceField(widget=forms.RadioSelect, choices=MY_CHOICES)

    def myfield_choices(self):
        """
        Returns myfield's widget's default renderer, which can be used to 
            render the choices of a RadioSelect widget.
        """
        field = self['myfield']
        widget = field.field.widget

        attrs = {}
        auto_id = field.auto_id
        if auto_id and 'id' not in widget.attrs:
            attrs['id'] = auto_id

        name = field.html_name

        return widget.get_renderer(name, field.value(), attrs=attrs)

Then in the template you can access individual radio button choices like so:

<ul>
    <li>
        {{ myform.myfield_choices.0 }}
        My custom HTML that goes with choice 0 
    </li>
    <li>
        {{ myform.myfield_choices.1 }}
        Different HTML that goes with choice 1 
    </li>
</ul>

or

{% for choice in myform.myfield_choices %}
    <div>
        {{ choice }}
    </div>
{% endfor %}

I'm pretty sure this is a bad idea. It will likely stop working at some point as django evolves. It violates DRY by copying code from as_widget(). (TBH, I didn't take the time to fully understand the code from as_widget) I'm using it as a temporary hack only. Perhaps there is a better way that involves custom template tags. If so, please let me know.

这篇关于在Django表单中,如何渲染单选按钮,使选择在页面上分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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