2014-10-16 34 views
0

我已经为Django中的表单字段编写了一个自定义小部件,而且我看到了我想要的那种HTML输出,但该字段未被返回到cleaned_data使用自定义小部件时丢失了Django表单字段数据

HTML是页面中表单的一部分,所以我的小部件有问题吗?

# forms.py 

class EntrantDetails(forms.Form): 
    TITLE_CHOICES = (
     ('', 'Select:'), 
     ('Mr', 'Mr'), 
     ('Mrs', 'Mrs'), 
     ('Miss', 'Miss'), 
     ('Ms', 'Ms'), 
     ('Dr', 'Dr'), 
    ) 
    GENDER_CHOICES = (
     ('M', 'male'), 
     ('F', 'female'), 
    ) 
    title = forms.CharField(
     max_length=20, 
     widget=forms.widgets.Select(choices=TITLE_CHOICES) 
    ) 
    first_name = forms.CharField(max_length=200) 
    middle_names = forms.CharField(
     label='Middle name(s)', 
     max_length=200, required=False 
    ) 
    last_name = forms.CharField(max_length=200) 
    date_of_birth = forms.DateField(
     widget=SelectDateWidget(years=range(2015, 1900, -1)) 
    ) 
    gender = forms.CharField(
     max_length=1, 
     widget=ButtonSelectWidget(cls='test_class', choices=GENDER_CHOICES) 
    ) 


# widgets.py 

class ButtonSelectWidget(Widget): 
    """ 
    Custom widget to display a choice field more like selectable buttons. 
    """ 

    def __init__(self, attrs=None, cls=None, choices=None): 
     self.attrs = attrs or {} 
     self.cls = cls 
     self.choices = choices 

    def render(self, name, value, attrs=None, choices=()): 
     if not choices: 
      choices = self.choices 
     output = [] 
     output.append(u'<div class="controls">') 

     for choice in choices: 
      label = u'{}'.format(choice[1]) 
      output.append(u''' 
      <div class="radio radio-{label}"> 
       <input id="user_{name}_{label}" name="user[{name}]" type="radio" value="{choice}"> 
       <label for="user_{name}_{label}">{title}</label> 
      </div> 
      '''.format(name=name, choice=choice[0], label=label, title=label.title())) 
     output.append(u'</div>') 


     return mark_safe(u'\n'.join(output)) 

回答

1

标记中的输入名称是错误的,所以表单不会收集它。取而代之的

<input id="user_{name}_{label}" name="user[{name}]" type="radio" value="{choice}"> 

你需要

<input id="user_{name}_{label}" name="{name}" type="radio" value="{choice}"> 

同时适用于Django表单控件id标准方案是id_<name>[_counter]

现在的Django已经有一个RadioSelect小部件,让你同样的功能,所以你会更好地使用它(在模板中使用你自己的特定标记),而不是在小部件中重新创建(平方)轮和硬编码项目的特定模板。

+0

啊,这是有道理的。我决定采用这种方法,因为模板已经是一个怪物,因为它是一个表单向导组窗体的单个模板,我想避免进一步的条件块。 – 2014-10-16 15:04:31

+1

您不需要条件来处理向导中的每个表单模板选择......只需在上下文中传递正确的表单模板路径,并在它上面传递“{%include%}”即可。 wrt/fields渲染,你可以1.使用默认标记或2.使用宏(https://pypi.python.org/pypi/django-templates-macros)或者一些更高级的软件包如软盘来应用你自己的标记到处。我的2分钱... – 2014-10-16 15:12:04

相关问题