I have a model:
class Survey(models.Model):
name = models.CharField(max_length = 200)
def __str__(self):
return self.name
And in my template I want to show the name of the current Survey model:
<h1> {{survey.name |name}} </h1>
I'm using a custom template tag/filter to display that name; however, it is showing as 'str' instead of the name of the current Survey model.
Here is the filter code:
from django import template
register = template.Library()
@register.filter
def name(value):
return value.__class__.__name__
What am I doing wrong here?
When you are calling the template tag with
<h1> {{survey.name |name}} </h1>you are passing it the string associated with the model. As the field is aCharFieldit will return a string and therefore the name of the that class isstr.I believe what you are wanting it
<h1> {{survey|name}} </h1>which will send the template tag the instance ofSurveyand therefore get the class name from that.