我在"admin.py">中有下面的代码:
# "admin.py"
from django.contrib.admin.widgets import AdminFileWidget
class CustomAdminFileWidget(AdminFileWidget):
def render(self, name, value, attrs=None, renderer=None):
result = []
if hasattr(value, "url"):
result.append(
f'''<a href="{value.url}" target="_blank">
<img
src="{value.url}" alt="{value}"
width="100" height="100"
style="object-fit: cover;"
/>
</a>'''
)
result.append(super().render(name, value, attrs, renderer))
return mark_safe("".join(result))
然后,我得到了下面的错误:
NameError: name 'mark_safe' is not defined
我要导入"mark_safe">但是我不知道在哪里可以找到"mark_safe">来自:
from <I_do_not_know> import mark_safe
哪里可以找到"mark_safe">来自哪里?
从django.utils.safestring
导入mark_safe:
from django.utils.safestring import mark_safe