【发布时间】:2020-04-20 06:46:25
【问题描述】:
我正在将图像上传到我的服务器(现在它只是 localhost)。以下是我的views.py 中导致问题的部分代码。
valid_extensions = ['jpg', 'jpeg', 'png', 'gif']
images = request.data.getlist('images[]') #Error here I believe
directory = os.path.join(os.getcwd(), 'static', 'images', 'products')
for image in images:
extension = os.path.splitext(image.name)[1][1:].lower()
file_name = "".join(choice(ascii_lowercase) for i in range(16)) + "." + extension
if extension in valid_extensions:
file_path = os.path.join(directory, file_name)
with open(file_path, 'wb+') as destination:
for chunk in image.chunks():
destination.write(chunk)
ProductImage.objects.create(file_name=file_name, original_name=image.name, file_length=image.size, product=product, file_path=file_path.replace(os.getcwd(), '').replace('\\', '/'))
else:
data = {'full_messages': ['Image file type is not supported.']}
return Response(data, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
data = {'full_messages': ['Product created successfully.']}
return Response(data, status=status.HTTP_201_CREATED)
由于某种原因,当我通过 Postman 发送我的 post 请求时,它成功上传了文件,但是当我通过 HTTP (Angular 8) 发送相同的请求时,我在控制台中收到错误。
上面写着AttributeError: 'NoneType' object has no attribute 'decode',如果我改变了
编辑(包括回溯)
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 65, in __call__
return self.application(environ, start_response)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 141, in __call__
response = self.get_response(request)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 75, in get_response
response = self._middleware_chain(request)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
response = response_for_exception(request, exc)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 91, in technical_500_response
text = reporter.get_traceback_text()
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 340, in get_traceback_text
c = Context(self.get_traceback_data(), autoescape=False, use_l10n=False)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 305, in get_traceback_data
'filtered_POST_items': list(self.filter.get_post_parameters(self.request).items()),
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 177, in get_post_parameters
return request.POST
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 110, in _get_post
self._load_post_and_files()
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/http/request.py", line 315, in _load_post_and_files
self._post, self._files = self.parse_file_upload(self.META, data)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/http/request.py", line 274, in parse_file_upload
parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/http/multipartparser.py", line 72, in __init__
raise MultiPartParserError('Invalid boundary in multipart: %s' % boundary.decode())
AttributeError: 'NoneType' object has no attribute 'decode'
【问题讨论】:
-
请显示完整的回溯,以及引发错误的行。
-
@ReinstateMonica 完成。
-
使用 Angular 发出的
POST请求是否使用Content-Type标头“multipart/form-data”执行? -
@OluwafemiSule 是的。
addProduct(status: number, item_type: number, name: string, description: string, price: number, stock: number, tags: string, categories: string, images: any) { return this.http.post<any>(PRODUCTS_API_URL + '/add/', {'name': name, 'item_type': item_type, 'status': status, 'description': description, 'price': price, 'stock': stock, 'tags[eeeeee]': tags, 'categories[dasdsa]': categories, 'images[]': images}, { headers: {'Authorization': 'Bearer ' + localStorage.getItem('jwt'), 'Content-Type': 'multipart/form-data'} }); }
标签: python django http django-rest-framework postman