相关疑难解决方法(0)

django-rest-framework http在django 1.5上以415失败

我正在使用REST API的django-rest-framework(最新版),并使用内置的测试客户端在django中实现了很少的测试用例.

以下django测试用例与django版本<1.5一起工作正常

self.client.put('/core/accounts/%s/'% self.account.id,
        data = prepare_dict(self.account),
        HTTP_AUTHORIZATION=self.token)
Run Code Online (Sandbox Code Playgroud)

升级到django 1.5,除了与HTTP PUT相关的测试外,所有测试都在通过, 同时调查发现的问题@ https://docs.djangoproject.com/en/dev/releases/1.5/#options-put-and-delete-请求合的测试客户端

如果在没有content_type的PUT请求中使用data参数,则必须先对数据进行编码,然后再将其传递给测试客户端并设置content_type参数.

因此,更新了我的测试以反映此更改并尝试了以下,但仍然获得http 415而不是http 200

from django.test.client import MULTIPART_CONTENT, BOUNDARY, encode_multipart
self.client.put('/core/accounts/%s/'% self.account.id,
            data = encode_multipart(BOUNDARY, prepare_dict(self.account)),
                content_type=MULTIPART_CONTENT,
        HTTP_AUTHORIZATION=self.token)
Run Code Online (Sandbox Code Playgroud)

知道我错过了什么吗?PS:django-rest-framework内置Web UI的所有功能都运行良好

django django-rest-framework

32
推荐指数
2
解决办法
1万
查看次数

权限被拒绝写入我的临时文件

我正在尝试使用Python在Windows操作系统上创建和写入临时文件.我使用Python模块tempfile创建了一个临时文件.

但是当我去写那个临时文件时,我收到了一个错误Permission Denied.我不允许写临时文件吗?!难道我做错了什么?如果我想创建和写入临时文件,我应该如何在Python中执行此操作?我想在临时目录中创建临时文件以用于安全目的而不是在本地(在.exe执行的目录中).

IOError: [Errno 13] Permission denied: 'c:\\users\\blah~1\\appdata\\local\\temp\\tmpiwz8qw'

temp = tempfile.NamedTemporaryFile().name
f = open(temp, 'w') # error occurs on this line
Run Code Online (Sandbox Code Playgroud)

python temporary-files

22
推荐指数
4
解决办法
2万
查看次数

如何使用 PUT 在 Django rest 框架中测试文件上传?

我想为 Django REST Framework 应用程序上的视图编写单元测试。测试应该使用 PUT 上传文件,本质上相当于

http -a malkaouri PUT http://localhost:8000/data-packages/upload/ka @tmp/hello.py

到目前为止我写的代码是

factory = APIRequestFactory()
request = factory.put(                                                                                                                                                                '/data-packages/upload/ka',
    data,
    content_type='application/octet-stream',
    content_disposition="attachment; filename=data.dump")
force_authenticate(request, user)
view = PackageView.as_view()
response = view(request, "k.py")
Run Code Online (Sandbox Code Playgroud)

显然,它不会上传文件。运行测试时的具体错误是400:

{u'detail': u'缺少文件名。请求应包含一个带有文件名参数的 Content-Disposition 标头。'}

值得注意的是,我使用请求工厂来测试视图而不是完整的客户端。这就是使这个问题中的解决方案对我不起作用的原因。

设置内容处置标头的正确方法是什么?

python django django-rest-framework pytest-django

8
推荐指数
2
解决办法
4577
查看次数

UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 14 中的字节 0xb9:起始字节无效

我正在使用 Django REST 进行文件上传测试。
Python3.6.2
Django1.11
djangorestframework==3.6.4
Excel-OSX 15.38(170902)
OSX 10.12.6

过去使用普通照片文件可以成功完成。
这次是来自网站的 Excel 文件。这是我从参考文献中得到的测试用例副本。

 def test_upload_and_process_data_complete_case(self):
        from django.core.files import File
        from django.core.files.uploadedfile import SimpleUploadedFile
        from soken_web.apps.imported_files.models import ImportFile

        file = File(open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx'))
        uploaded_file = SimpleUploadedFile('new_image.xlsx', file.read(), content_type='multipart/form-data')

        data = {
            'attribute': {'author': 'Sigh'},
            'type': ImportFile.FileType.zipcode,
            'file': uploaded_file
        }
        response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)
Run Code Online (Sandbox Code Playgroud)

像个抄袭猫。除了这次我从https://www.mockaroo.com/下载了一个模拟文件。

这是我执行时引发的错误 file.read()

file
<File: /Users/el/Code/norak-cutter/soken/soken-web/soken_web/apps/zipcodes/complete.xlsx>
file.read()
Traceback (most recent call last):
  File "/Users/el/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/172.3968.37/PyCharm.app/Contents/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, …
Run Code Online (Sandbox Code Playgroud)

python django macos excel

4
推荐指数
1
解决办法
3469
查看次数

DRF APITestCase 不将“multipart”与其他参数一起使用

我有 2 个型号。首先是House。其次,HouseImage
因此我必须提交图像,ForeigneKey
我可以正常使用 REST 上传,但无法进行单元测试。
我之所以继续在这里进行单元测试,是因为我有更多的规范在等着我,我肯定不会进行手工测试。

django==1.11.5
djangorestframework==3.6.4
python3.6.2
PostgreSQL 9.6.5 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.0 (clang-700.1.76), 64-bit

这是我的附加源代码。
https://gist.github.com/elcolie/a013be9c3b7ab5f0cc130e320b19da4b

导入临时文件

from PIL import Image
from django.contrib.auth.models import User
from model_mommy import mommy
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase, APIClient

from soken_web.apps.houses.models import House


class HouseImageTest(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = mommy.make(User, username='Pan')
        self.house = mommy.make(House, location="100.00, 100.00")

    def test_post_image(self):
        self.client.force_authenticate(user=self.user)

        image = …
Run Code Online (Sandbox Code Playgroud)

python django rest

3
推荐指数
1
解决办法
1078
查看次数