我要更改用户个人资料照片;但是我不知道如何增加上传大小限制
views.py
def profile_photo_upload(request):
if request.method=="POST":
form=UploadForm(request.POST, files=request.FILES,instance=request.user.profile)
if form.is_valid():
userprofile=form.save(commit=False)
userprofile.user=request.user
userprofile.save()
messages.success(request,"Success")
else:
form=UploadForm()
return render(request,'imageupdate.html',{'form':form})
另外,我的代码是否正确?为了安全
答案 0 :(得分:0)
from django.core.exceptions import ValidationError
def file_size(value): # add this to some file where you can import it from
limit = 2 * 1024 * 1024
if value.size > limit:
raise ValidationError('File too large. Size should not exceed 2 MB.')
并在您的form.py中添加它。
image = forms.FileField(validators=[file_size])