您好,登录后才能下订单哦!
这篇文章主要为大家展示了“Python中Django框架单元测试之文件上传测试的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Python中Django框架单元测试之文件上传测试的示例分析”这篇文章吧。
具体如下:
Submitting files is a special case. To POST a file, you need only provide the file field name as a key, and a file handle to the file you wish to upload as a value. For example:
>>> c = Client() >>> with open('test.jpg') as fp: ... c.post('/account/avatar_upload/',{'avatar':fp})
测试文件上传其实没有什么特殊的,只需要指定后端接受请求数据的对应键值即可
(The name avatar here is not relevant; use whatever name your file-processing code expects.)在这里avatar是关联的,对应着具体的后端处理程序代码,eg:
class Useravatar(View): def __init__(self): self.thumbnail_dir = os.path.join(STATIC_ROOT, 'avatar/thumbnails') self.dest_dir = os.path.join(STATIC_ROOT, 'avatar/origin_imgs') @method_decorator(login_required) def post(self, request): nt_id = request.session.get('user_id', 'default') user = User.objects.get(pk=nt_id) if User.objects.filter(pk=nt_id).exists() else None avatarImg = request.FILES['avatar'] if not os.path.exists(self.dest_dir): os.mkdir(self.dest_dir) dest = os.path.join(self.dest_dir, nt_id+"_avatar.jpg") with open(dest, "wb+") as destination: for chunk in avatarImg.chunks(): destination.write(chunk) if make_thumb(dest,self.thumbnail_dir): avartaPath = os.path.join(STATIC_URL, 'avatar/thumbnails', nt_id + "_avatar.jpg") else: avartaPath = os.path.join(STATIC_URL, 'avatar/origin_imgs', nt_id + "_avatar.jpg") User.objects.filter(nt_id=nt_id).update(avatar=avartaPath) return render(request, 'profile.html', {'user': user})
以上是“Python中Django框架单元测试之文件上传测试的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。