Django自定义用户模型图像字段显示在用户列表中

huangapple go评论77阅读模式
英文:

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,)

如何在用户表的列表视图中显示头像图片?

在用户列表中没有看到头像,但没有报错。

这是图片
Django自定义用户模型图像字段显示在用户列表中

英文:

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&quot;images/avatar/{self.user.id}/{filename}&quot;
    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(&#39;&lt;img src=&quot;{}&quot; height=&quot;50&quot;/&gt;&#39;.format(self.avatar.url))
        else:
            return &quot;&quot;
    avatar_tag.short_description = &#39;Image&#39;
    avatar_tag.allow_tags = True
    full_name = &#39;&#39;
   
    bio    = models.TextField(default=&quot;&quot;, 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 = &#39;user&#39;
    can_delete = False
    verbose_name = &quot;User profile&quot;
    verbose_name_plural = &#39;Profiles&#39;
    classes = (&#39;text-capitalize&#39;,&#39;collapse open&#39;)
    extra = 1
    list_display = (&#39;avatar_tag&#39;)
    fieldsets =  (
        (&quot;Profile&quot;, {&#39;fields&#39;: (&#39;avatar&#39;,&#39;bio&#39;, &#39;display&#39;,)}),)


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

Here is the image
Django自定义用户模型图像字段显示在用户列表中

答案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 = (&quot;avatar_tag&quot;,) # pass other fileds if you want eg. name

    def avatar_tag(self, obj):
        if obj.userprofile.avatar:
            return format_html(
                &#39;&lt;img src=&quot;%s&quot;/&gt;&#39; % obj.userprofile.avatar.url
            )
        return &quot;No avatar&quot;

list_display accepts callable argument so you can pass custom method names in it.

huangapple
  • 本文由 发表于 2023年5月17日 14:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定