英文:
Django CustomUser model Image field display in User List
问题
UserProfile
是一个 CustomUser 模型,拥有 avatar、bio 和 display 字段。
我想在用户列表中显示个人资料的头像。
我已经添加了一个名为 avatar_tag 的函数,供用户列表调用。
#models.py
class UserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
def get_upload_avatar_to(self, filename):
return f"images/avatar/{self.user.id}/{filename}"
avatar = models.ImageField(upload_to=get_upload_avatar_to, null=True, blank=True)
def avatar_tag(self):
if self.avatar.url is not None:
return mark_safe('<img src="{}" height="50"/>'.format(self.avatar.url))
else:
return ""
avatar_tag.short_description = 'Image'
avatar_tag.allow_tags = True
full_name = ''
bio = models.TextField(default="", blank=True)
display = models.CharField(default=full_name,max_length=512,blank=True)
在 admin.py 中的代码如下:
class UserProfileInline(admin.StackedInline):
model = UserProfile
fk_name = 'user'
can_delete = False
verbose_name = "User profile"
verbose_name_plural = 'Profiles'
classes = ('text-capitalize','collapse open')
extra = 1
list_display = ('avatar_tag',)
fieldsets = (
("Profile", {'fields': ('avatar','bio','display',)}),)
class UserProfileAdmin(UserAdmin):
inlines = (UserProfileInline,)
如何在用户表的列表视图中显示头像图片?
在用户列表中没有看到头像,但没有报错。
英文:
UserProfile
is CustomUser Model having columns avatar, bio and display fields.
I want to show Profile avatar in the User List
I have added avatar_tag function to be called by the users list
#models.py
class UserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
def get_upload_avatar_to(self, filename):
return f"images/avatar/{self.user.id}/{filename}"
avatar = models.ImageField(upload_to=get_upload_avatar_to, null=True, blank=True)
def avatar_tag(self):
if self.avatar.url is not None:
return mark_safe('<img src="{}" height="50"/>'.format(self.avatar.url))
else:
return ""
avatar_tag.short_description = 'Image'
avatar_tag.allow_tags = True
full_name = ''
bio = models.TextField(default="", blank=True)
display = models.CharField(default=full_name,max_length=512,blank=True)
admin.py code is below
class UserProfileInline(admin.StackedInline):
model = UserProfile
fk_name = 'user'
can_delete = False
verbose_name = "User profile"
verbose_name_plural = 'Profiles'
classes = ('text-capitalize','collapse open')
extra = 1
list_display = ('avatar_tag')
fieldsets = (
("Profile", {'fields': ('avatar','bio', 'display',)}),)
class UserProfileAdmin(UserAdmin):
inlines = (UserProfileInline,)
How can I show the avatar image in list view of the User table?
I am not getting any errors in the user list avatar is not shown
答案1
得分: 1
从django.utils.html导入format_html
class UserProfileAdmin(UserAdmin):
list_display = ("avatar_tag",) # 如果你想要的话,可以传递其他字段,比如姓名
def avatar_tag(self, obj):
if obj.userprofile.avatar:
return format_html(
'<img src="%s"/>' % obj.userprofile.avatar.url
)
return "无头像"
英文:
Try this code
from django.utils.html import format_html
class UserProfileAdmin(UserAdmin):
list_display = ("avatar_tag",) # pass other fileds if you want eg. name
def avatar_tag(self, obj):
if obj.userprofile.avatar:
return format_html(
'<img src="%s"/>' % obj.userprofile.avatar.url
)
return "No avatar"
list_display
accepts callable argument so you can pass custom method names in it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论