2009-12-31 126 views
1

我想要在标签中得到“*”。有点接近这个自定义django表单域

<label for="id_name"><strong>Name</strong> <em>*</em></label> 

随着label_tag

{{ field.label_tag }} 

它产生的

<label for="id_city">City</label> 

这个开始和关闭标签标记,如何 “*” 关闭

前插入

这个黑客似乎工作,

<label for="id_{{ field.label }}">{{ field.label }} 
{% if field.field.required %}<em>*</em>{% endif %}</label> 

它并不像字段标签的ID会比字段标签不同的功能,如“名”不“名”

回答

2

你的代码是非常相似的this Django snippet。有意见有建议使用:

<label for="{{ field.auto_id }}"> 

你的,而不是:

<label for="id_{{ field.label }}"> 

您也可以尝试this snippet作为替代。

0

模板:

<label for="{{ user_form.first_name.id_for_label }}" class="{{ user_form.first_name.css_classes }}">{{ user_form.first_name.label }}{{ user_form.label_suffix }}</label> 

forms.py:

class UserChangeForm(DjangoUserChangeForm): 
    error_css_class = 'field_error' 
    required_css_class = 'field_required' 

CSS:

.field_required:after { 
    content: " *"; 
}