如何为用户设置默认头像

时间:2017-03-23 19:54:01

标签: django templates model

当用户没有放置自己的图片时,如何为用户设置默认头像?

这是我的模板。

ifequal声明不起作用。

{% for each_model in Result %}

<div class="panel panel-success" style="margin-left:10px;margin-right:50px ;margin-top:100px;width:400px ;float:right">
        <div class="panel-heading">
          <h3 class="panel-title"style="text-align:center"><strong>USER INFORMATION</strong></h3>
        </div>
        <div class="panel-body">

         {% ifequal each_model.Profile_image blank %}
           <img src="{{MEDIA_URL}}/Profile_image/deafult-profile-image.png"/>
                <hr>
            {% else %}
                 <img src="{{MEDIA_URL}}{{each_model.Profile_image}}"style="width:300px; height:200px; display:block; margin-left:auto;margin-right:auto">
                    <hr>
        {% endifequal %}

这是我的模特

我试图在字段中输入默认值,但它无效。

class MyUser(AbstractBaseUser):
   email = models.EmailField(
    verbose_name='Email',
    max_length=255,
    unique=True,null = False
)
username = models.CharField(max_length = 30, null = False)

Nationality =models.CharField(max_length = 30,choices= Country_choice,null = False )
Mother_language = models.CharField(max_length = 30,choices= Language_list,null = False)
Wish_language =models.CharField(max_length = 30,choices= Language_list,null = False)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
Profile_image = models.ImageField(upload_to='profile_images',blank=True, default= 'profile_images/deafult-profile-image.png')
status_message=models.CharField(max_length = 500,null = True)
objects = MyUserManager()



USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username','Nationality','Mother_language','Wish_language','Profile_image','status_message']

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

getAttribute()

但我建议您为您的用户模型设置一个方法,该方法将返回用户头像并在模板中使用该方法:

 {% if not each_model.Profile_image %}
       <img src="{{MEDIA_URL}}/Profile_image/deafult-profile-image.png"/>
            <hr>
        {% else %}
             <img src="{{MEDIA_URL}}{{each_model.Profile_image}}"style="width:300px; height:200px; display:block; margin-left:auto;margin-right:auto">
                <hr>
    {% endifequal %}