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/form_tags.py
from django import template

register = template.Library()

@register.simple_tag
def get_model_name(form):
    return form._meta.model.__name__
                  
   
            

Related Posts