使用uuid唯一标识码
#文件上传1:写到服务器中
file_hpic = request.FILES['hpic']
file_name = mytools.do_file_name(file_hpic.name)
file_path = os.path.join(settings.MEDIA_ROOT,'book/images',file_name)
with open(file_path,'wb') as file:
for chunk in file_hpic.chunks():
file.write(chunk)
#文件上传2:写入到数据库中
heroinfo.hpic = os.path.join('book/images',file_name)
#mytools.py文件下的
def do_file_name(file_name):
return str(uuid.uuid1()) + os.path.splitext(file_name)[1]