Reputation:
This question is similar to this question (Model name of objects in django templates) but not the same.
I want to know how gets the Models class name (in this case 'Apple'), or at the very least return a string that I can pass over.
Model.py
django.db import models
class Apple(models.Model):
.....
apple.html
<p>Sorry, no {{ ***Model class name**** }}</p>
An example in the browser::: " Sorry, no Apple "
EDIT
Some extra conditions:
Upvotes: 2
Views: 3031
Reputation: 6116
If you're passing an object to your template, and you want to do this processing in the template and not in the views, one approach is to create the following template tag:
@register.filter
def get_class(value):
return value.__class__.__name__
and get the name with
{{apple|get_class}}
Upvotes: 5