Model name of objects in django templates

前端 未结 5 1575
遥遥无期
遥遥无期 2020-12-09 07:59

Is there any way to get the model name of any objects in django templates. Manually, we can try it by defining methods in models or using template tags... But is there any b

5条回答
  •  攒了一身酷
    2020-12-09 08:22

    You cannot access the class name directly. Doing something like this:

    {{ object.__class__ }}
    

    will cause a TemplateSyntaxError: Variables and attributes may not begin with underscores. Django doesn't let you access those sorts of attributes - Python conventions means they are hidden implementation details, not part of the object's API.

    Create a template filter instead, and then you can use it as follows:

    {{ object|model_name_filter }}
    

    Creating filters is very easy: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

提交回复
热议问题