这是我的模特:
class CalEvents(models.Model):
user = models.ForeignKey(User)
activity_name = models.CharField(max_length=256, unique=False, blank=True)
activity_type = models.CharField(max_length=30, unique=False, blank=True)
activity_code = models.CharField(max_length=30, unique=False, blank=True)
def __str__(self):
return "Activity: '{}' - @{}".format(self.activity_name, self.user.username)
这是我的forms.py(我试图定义标签的解决方案):
from django.forms import ModelForm
from gCalData.models import CalEvents
from accounts.models import User
class CalEventsForm(ModelForm):
class Meta:
model = CalEvents
fields = ['activity_name','activity_type','activity_code']
labels = {"actiity_name": "Activity name",
"activity_type": "Activity type",
"activity_code": "Activity code"}
def __init__(self, *args, **kwargs):
super(CalEventsForm, self).__init__(*args, **kwargs)
self.fields['activity_name'].label = "Activity name"
self.fields['activity_type'].label = "Activity type"
self.fields['activity_code'].label = "Activity code"
这是我放置表单的模板的一部分:
<tr>
<form method="POST">
{% csrf_token %}
{% for field in form %}
<td>{{ field }}</td>
{% endfor %}
<td><input type="submit" name="new_record" class="btn btn-primary" value="Add Activity"></td>
</form>
</tr>
我做错了什么?
答案 0 :(得分:2)
尝试添加{{ field.label_tag }}
(您决定的html包装):
{% for field in form %}
{{ field.label_tag }}
<td>{{ field }}</td>
{% endfor %}
修改强>
您还可以添加错误标记:
{% for error in field.errors %}
<div class="alert alert-danger" role="alert">{{ error|escape }}</div>
{% endfor %}