I’m currently writing a Django template template tag, and I would like to be able to reference a class in it with a string, just like in settings.MIDDLEWARE_CLASSES, for example. The tag would look like {% mytag "myproject.lib.mymodule.SomeClass" %}.
The question is fairly simple : in my template tag, how may I easily resolve this string to the actual referenced class?
Thanks.
If you really need to import a package specified as a string, you will need to check out the
__import__function.Basic usage is that you can attempt to import the package specified in the string like
which is equivalent to
However, as S.Lott mentioned, you’ll need to consider carefully if that’s really the best way to solve the problem.