2009-12-31 125 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: " *"; 
}