擅长:python、mysql、java
<p>我用<code>Python 3.4</code>在<code>Django</code>中得到这个错误。我想让这个和<a href="http://www.django-rest-framework.org/" rel="nofollow">django-rest-framework</a>一起工作。</p>
<p>这是我的代码,它修复了unicodedecoderror:“utf-8”编解码器无法解码字节错误。</p>
<p>这是通过测试:</p>
<pre><code>import os
from os.path import join, dirname
import uuid
from rest_framework.test import APITestCase
class AttachmentTests(APITestCase):
def setUp(self):
self.base_dir = dirname(dirname(dirname(__file__)))
self.image = join(self.base_dir, "source/test_in/aaron.jpeg")
self.image_filename = os.path.split(self.image)[1]
def test_create_image(self):
id = str(uuid.uuid4())
with open(self.image, 'rb') as data:
# data = data.read()
post_data = {
'id': id,
'filename': self.image_filename,
'file': data
}
response = self.client.post("/api/admin/attachments/", post_data)
self.assertEqual(response.status_code, 201)
</code></pre>