'NameError: name 'mark_safe' is not defined (Django)

I have this code below in "admin.py":

# "admin.py"

class AdminImageWidget(AdminFileWidget):
    def render(self, name, value, attrs=None, renderer=None):
        output = []

        if value and getattr(value, "url", None):
            image_url = value.url
            file_name = str(value)
            
            output.append(
                f'<a href="{image_url}" target="_blank">'
                f'<img src="{image_url}" alt="{file_name}" width="150" height="150" '
                f'style="object-fit: cover;"/> </a>')
                
        output.append(super(AdminFileWidget, self).render(name, value, attrs, renderer))
        return mark_safe(u''.join(output))

Then, I got this error below:

NameError: name 'mark_safe' is not defined

So, I want to import "mark_safe" but I don't know where I can get the "mark_safe" from:

from <I_do_not_know> import mark_safe

Where can I get "mark_safe" from?



Solution 1:[1]

Improt "mark_safe" from "django.utils.safestring" in "admin.py" as shown below:

# "admin.py"

from django.utils.safestring import mark_safe

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kai - Kazuya Ito