Django template tag to return model form model name as string

Django -- Posted on Aug. 8, 2023

If you want to retrieve the model name associated with a ModelForm in Django, you can do so by accessing the Meta class of the form. Here's how you can achieve this:

                # templatetags/
from django import template

register = template.Library()

def get_model_name(form):
    return form._meta.model.__name__

Related Posts