在django中,如何在表单类中添加锚标记而不自定义form.html
class HealthCheckerForm(PortalModelForm):
name = forms.CharField(max_length=128,required = False ,help_text="Enter Name", label='Name')
description = forms.CharField(max_length=128, required = False, label='desc*')
age = forms.CharField(max_length=128,required = False ,help_text="Enter age", label='age')
group = forms.CharField(max_length=128, required = False, label='group*')
{% for field in form %}
<div class="fieldWrapper">
<div class="fieldInfo">
{{ field.label_tag }}{{ field }}
<span class="errorList">{{ field.errors|striptags}}</span>
</div>
</div>
{% endfor %}
没有自定义form.html如何添加按钮或锚标记
提前致谢..
答案 0 :(得分:1)
我希望我理解你的问题;它似乎与我今天遇到的问题类似。
基本上你可以创建自己的Widget和Field。
from django.utils.safestring import mark_safe
class AnchorOnlyWidget(forms.Widget):
def render(self, name, value, attrs):
final_attrs = self.build_attrs(attrs, name=name)
if hasattr(self, 'initial'):
value = self.initial
return mark_safe("<a href='%s' %s>%s</a>" %
(value, flatatt(final_attrs), value))
def _has_changed(self, initial, data):
return False
class AnchorOnlyField(forms.FileField):
widget = AnchorOnlyWidget
def __init__(self, widget=None, label=None, initial=None, help_text=None):
forms.Field.__init__(self, label=label, initial=initial,
help_text=help_text, widget=widget)
def clean(self, value, initial):
self.widget.initial = initial
return initial
然后将其添加到您的表单中:
class HealthCheckerForm(PortalModelForm):
youranchor = AnchorOnlyField(help_text="the help")
...