最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Django框架單元測試之文件上傳測試示例

 更新時(shí)間:2019年05月17日 11:58:49   作者:Lockeyi  
這篇文章主要介紹了Python Django框架單元測試之文件上傳測試,結(jié)合實(shí)例形式分析了Django框架單元測試中文件上傳測試的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了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})

測試文件上傳其實(shí)沒有什么特殊的,只需要指定后端接受請求數(shù)據(jù)的對應(yīng)鍵值即可

(The name avatar here is not relevant; use whatever name your file-processing code expects.)在這里avatar是關(guān)聯(lián)的,對應(yīng)著具體的后端處理程序代碼,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})

希望本文所述對大家基于Django框架的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

连云港市| 峨眉山市| 外汇| 合阳县| 鄂伦春自治旗| 沂南县| 江北区| 昭通市| 日照市| 德保县| 东乌珠穆沁旗| 修文县| 新民市| 阿城市| 广饶县| 沙坪坝区| 五大连池市| 康定县| 肥城市| 曲麻莱县| 迁安市| 柏乡县| 民乐县| 万荣县| 延边| 元谋县| 上犹县| 青海省| 河津市| 河北区| 清新县| 富阳市| 太康县| 凤凰县| 安国市| 林口县| 麻阳| 抚宁县| 天镇县| 邯郸市| 彭水|