以下代码在Django 1.11中运行良好,但在DJango 2及以上版本中它失败了:
'imageToLoad' : imageQ[0].imagePath
错误为AttributeError: 'ImageFieldFile' object has no attribute 'replace'
imagePath是一个ImageField。
我正在阅读DJango 2文档,但无法弄清问题是什么。
以下是整个代码块:
def getContextFromObject(request,imageQ):
if (imageQ != None):
imageJson = serializers.serialize("json", imageQ)
print("image id is " + str(imageQ[0].id))
request.session['image_id'] = imageQ[0].id
print("attempt to load: " + str(imageQ[0].imagePath))
# Below is the block that fails on DJango 2. Specifically, it is the first part, 'imageToLoad' : imageQ[0].imagePath
context = {'imageToLoad' : imageQ[0].imagePath,
'imageWidthPixels': imageQ[0].widthPixels,
'imageHeightPixels': imageQ[0].heightPixels,
'imageMeasuredCenter': (imageQ[0].swMeasuredCenter).quantize(Decimal('.01'), rounding=ROUND_DOWN),
'micronsPerPixel': imageQ[0].micronsPerPixel,
'imageChannelSelect': imageQ[0].channelSelect,
'imageId' : str(imageQ[0].id),
'images' : imageJson
}
else:
context = { }
return context;
以下是完整的错误追溯:
Traceback (most recent call last):
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\staticfiles\handlers.py", line 66, in __call__
return self.application(environ, start_response)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\wsgi.py", line 146, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 81, in get_response
response = self._middleware_chain(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\vistagalaxy\images\views.py", line 717, in currentViewing
return render(request, template, context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\templatetags\static.py", line 106, in render
url = self.url(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\templatetags\static.py", line 103, in url
return self.handle_simple(path)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\templatetags\static.py", line 118, in handle_simple
return staticfiles_storage.url(path)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\files\storage.py", line 329, in url
url = filepath_to_uri(name)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\encoding.py", line 252, in filepath_to_uri
return quote(path.replace("\\", "/"), safe="/~!*()'")
AttributeError: 'ImageFieldFile' object has no attribute 'replace'
[14/Mar/2018 10:47:00] "GET /viewing HTTP/1.1" 500 59