Say I have a model:
from django.db import models
class Foo(models.Model)
fav_color = models.CharField(max_length=250, help_text="What's your fav color?")
print(Foo.fav_color.field.help_text) # prints: What's your fav color?
Say I have a view with context:
{
'Foo': Foo
}
And in a template {{ Foo.fav_color.field.help_text }}, the result will be blank. However {{ Foo }} will print the string representation of the model, so I know the models is getting passed into the template.
Why are the attributes look ups to get the help_text failing in the template, why is the help_text empty in the template?
Maybe the template engine is considering
Foo.fav_coloras a string instead of model field.When you pass the
Foomodel to the template, the string representation of the model that includes its class name and primary key value is automatically generated by Django. So, the moment you do{{ Foo }}, Django displays string representation of theFooobject, which is different from accessing thefav_colorfield.Solution:
You can directly pass
help_textin template so:Then, access
help_textin template only through{{fav_help_text}}Edit
You can define a
context processorto include theFoomodel instance in the context of every request so:Then add this to
TEMPLATESin settings.py so:Now, With this context processor, the
Foomodel instance will be included in the context of every request, then simply do the following in template: