Reputation: 2698
I have to following forms.py
and models.py
:
# models
class BHA_Component(models.Model):
item_description = models.CharField(max_length=111)
num_of_jts = models.CharField(max_length=111)
length = models.CharField(max_length=111)
cum_length = models.CharField(max_length=111)
outer_d = models.CharField(max_length=111)
# forms
class BHA_Component_Form(forms.ModelForm):
class Meta():
model = BHA_Component
fields = '__all__'
So far, I've been printing the field names on the screen using html like this:
{% for field in bha_form %}
<label>{{ field.name }}</label>
{% endfor %}
I passed in the form, bha_form
as a context variable in views.py
.
If I use this set of code, it this on a screen:
item_description, num_of_jts, length, cum_length, outer_d
But I want to print this on a screen:
Item Description, # of jts, Lenth, Cum. Len., OD
As you can see, I can't get this output by simply using some s.upper()
method, or regex method, because there no certain string transformation relationship between the original field names and the output I want.
I want to manually decide what field names will be printed on a screen for each field. But I don't want to do this by hard coding html elements like this:
<label>Item Description</label>
<label># of jts</label>
<label>Length</label>
I want to do this on the backend using Django. How should I go about this?
Upvotes: 3
Views: 8995
Reputation: 13542
Set a name on your fields. See verbose field names in the documentation.
class BHA_Component(models.Model):
item_description = models.CharField('Item Description', max_length=111)
The verbose name will automatically be picked up (and capitalized) by the form to create label text.
I cannot check right now, but if my memory is correct, I believe you should be using field.label
rather than field.name
in your label though.
Upvotes: 7