我正在尝试获取上传文件列表,同时获取从ReactJS FrontEnd到Django BackEnd的JSON对象,
在 views.py
中@csrf_exempt
def simple_upload(request):
if request.method == 'POST':
print("REQUEST POST: ", request.POST)
print("REQUEST META: ", request.META)
#received_json_data = json.loads(request.body.decode("utf-8"))
#print("JSON DATA: ",received_json_data)
#print(received_json_data)
gps = request.FILES['gps']
fs = FileSystemStorage()
filename_gps = fs.save(gps.name, gps)
print(fs.url(filename_gps))
gps_json = json.load(open(fs.url(filename_gps)[1:]))
print("GPS: ",gps_json)
在 my_template.html
中如果有一个下降区
<div class="dropzone">
<div class="content">
<img src="{% static 'core/img/upload.svg' %}" class="upload">
<span class="filename"></span>
<input type="file" name="myfile" class="input" multiple>
<input type="file" name="myjson" class="input">
</div>
</div>
编辑:
来自前端ReactJS的JSON对象:
var jsonGPS = {"latitude":123456,"longitude":123456}
form.append("gps",jsonGPS)
问题1:
[2018-08-17 20:51:25 +0000] [31996] [DEBUG] POST /uploads/simple/
Internal Server Error: /uploads/simple/
Traceback (most recent call last):
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/miguel/myapp/object-detection-uploader/uploads/core/views.py", line 36, in simple_upload
gps = request.FILES['gps']
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/utils/datastructures.py", line 85, in __getitem__
raise MultiValueDictKeyError(repr(key))
MultiValueDictKeyError: "'gps'"
问题2(应用Jerin解决方案后):
[2018-08-18 09:29:15 +0000] [24055] [DEBUG] POST /uploads/simple/
Internal Server Error: /uploads/simple/
Traceback (most recent call last):
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/miguel/virtualizacion/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/miguel/myapp/object-detection-uploader/uploads/core/views.py", line 31, in simple_upload
gps = json.loads(gps_str) # 'gps' as dict (python)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
这是格式问题吗?
答案 0 :(得分:1)
from django.http import HttpResponse
@csrf_exempt
def simple_upload(request):
gps_str = request.POST.get('gps', None)
if gps_str is None:
return HttpResponse("'gps' key not found in request payload")
gps = json.loads(gps_str) # 'gps' as dict (python)
files = request.FILES # you will get all files as dict like object
return HttpResponse("some response")