我正在使用 Django 上传用户头像,我的问题是,如何从模板中删除默认字符串“urrently: avatar/default.png Clear”
模型:
class User(AbstractUser):
nickname1 = models.CharField(max_length=30, blank=True, null=True, verbose_name='昵称')
url = models.URLField('个人网址', blank=True, help_text='提示:网址必须填写以http开头的完整形式')
avatar = ProcessedImageField(upload_to='avatar', default='avatar/default.png', verbose_name='头像',
processors=[ResizeToFill(100, 100)], # 处理后的图像大小
format='JPEG', # 处理后的图片格式
options={'quality': 95} # 处理后的图片质量
, blank=True
)
identifier = models.CharField(max_length=40, unique=True,blank=True, null=True)
...
USERNAME_FIELD = 'identifier'
def save(self, *args, **kwargs):
if len(self.avatar.name.split('/')) == 1:
self.avatar.name = self.username + '/' + self.avatar.name
super(User, self).save()
class Meta:
verbose_name = '用户信息'
verbose_name_plural = verbose_name
ordering = ['-id']
def __str__(self):
return self.username
网址:
path('<str:username>/', views.account_profile,name = 'process'),
视图.py:
@login_required
def account_profile(request, username):
profile_user = get_object_or_404(User, username=username)
messages = []
if request.method == 'POST':
form = UserDetailForm(request.POST, request.FILES, instance=request.user)
if form.is_valid():
form.save()
messages.append('资料修改成功!')
form = UserDetailForm(instance=request.user)
return render(request, 'user/user_detail.html', context={'form': form,
'messages': messages,
'profile_user': profile_user,})
形式:
class UserDetailForm(ModelForm):
class Meta:
model = User
fields = ['avatar',]
模板:
<form class="profile" method="post" enctype="multipart/form-data"
action="/<str:username>/">
{% csrf_token %}
{{ form|crispy }}
<div class="avatar">
<img class="img-rounded" src="{{ request.user.avatar.url }}">
</div>
<button class="primaryAction" type="submit">更新资料</button>
</form>
在表单中我只在那里设置了归档头像,但总是有一个字符串“当前:头像/default.png清除”
有朋友知道怎么去掉这个字符串吗?