Django表单域选择,添加一个属性 [英] Django form field choices, adding an attribute

查看:118
本文介绍了Django表单域选择,添加一个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  PLANNING_CHOICES =(
('0',u'Every morning'),
('1',u'Every night'),
('2',u'Never'),


planning = forms.ChoiceField(
required = True,
choices = PLANNING_CHOICES,

有一个这样的表单域名叫规划,我需要添加标题属性到选择,在结束呈现为:

 < select> 
< option value =0title =bla1>每天早上< / option>
< option value =1title =bla2>每天晚上< / option>
< option value =2title =bla3>从不< / option>
< / select>

如何实现?

解决方案

您必须对该字段进行子类化,以采取任何方式来指定所需的标题,并使用小部件显示新属性。



如果你有这样的事情(注意:完全未经测试):

  from django import forms 
from django.utils.html导入escape
from django.utils.encoding import force_unicode

class SelectWithTitles(forms.Select):
def __init __(self,* args,** kwargs )
super(SelectWithTitles,self).__ init __(* args,** kwargs)
#确保标题dict存在
self.titles = {}

def render_option(self,selected_choices,option_value,option_label):
title_html =(self.titles中的option_label)和\ $​​ b $ b u'title =%s'%escape(force_unicode(self.titles [ option_label]))或''
option_value = force_un icode(option_value)
selected_html =(selected_choices中的option_value)和u'selected =selected'或''
return u'< option value =%s%s%s>%s< ; / option>'%(
escape(option_value),title_html,selected_html,
conditional_escape(force_unicode(option_label)))

class ChoiceFieldWithTitles(forms.ChoiceField) $ b widget = SelectWithTitles

def __init __(self,choices =(),* args,** kwargs):
choice_pairs = [(c [0],c [1])for c in choice]
super(ChoiceFieldWithTitles,self).__ init __(choices = choice_pairs,* args,** kwargs)
self.widget.titles = dict([(c [1],c [2 ])for c in choices])

...你应该可以这样做: p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$''''''''''''''''''''''''''''''''''''''''''''''' 1','每天晚上','bla2'),
('2','Never','b la3'),


planning = forms.ChoiceFieldWithTitles(
required = True,choices = PLANNING_CHOICES_WITH_TITLES)


PLANNING_CHOICES = (
    ('0',u'Every morning'),
    ('1',u'Every night'),
    ('2',u'Never'),
)

    planning = forms.ChoiceField(
                                required=True,    
                                choices = PLANNING_CHOICES,
                                )

Having a such form field named planning, I need to add title attribute to choices and in the end renders as this:

                <select>
                    <option value="0" title="bla1">Every morning</option>
                    <option value="1" title="bla2">Every night</option>
                    <option value="2" title="bla3">Never</option>
                </select>

How can it be achieved ?

解决方案

You'd have to subclass the field to take whatever means of specifying the title you'd like and the widget to display the new attribute.

If you had something like this (note: entirely untested):

from django import forms
from django.utils.html import escape
from django.utils.encoding import force_unicode

class SelectWithTitles(forms.Select):
    def __init__(self, *args, **kwargs):
        super(SelectWithTitles, self).__init__(*args, **kwargs)
        # Ensure the titles dict exists
        self.titles = {}

    def render_option(self, selected_choices, option_value, option_label):
        title_html = (option_label in self.titles) and \
            u' title="%s" ' % escape(force_unicode(self.titles[option_label])) or ''
        option_value = force_unicode(option_value)
        selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
        return u'<option value="%s"%s%s>%s</option>' % (
            escape(option_value), title_html, selected_html,
            conditional_escape(force_unicode(option_label)))

class ChoiceFieldWithTitles(forms.ChoiceField):
    widget = SelectWithTitles

    def __init__(self, choices=(), *args, **kwargs):
        choice_pairs = [(c[0], c[1]) for c in choices]
        super(ChoiceFieldWithTitles, self).__init__(choices=choice_pairs, *args, **kwargs)
        self.widget.titles = dict([(c[1], c[2]) for c in choices])

...you should be able to do this:

PLANNING_CHOICES_WITH_TITLES = (
    ('0', 'Every morning', 'bla1'),
    ('1', 'Every night',   'bla2'),
    ('2', 'Never',         'bla3'),
)

planning = forms.ChoiceFieldWithTitles(
    required=True, choices=PLANNING_CHOICES_WITH_TITLES)

这篇关于Django表单域选择,添加一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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