Flask 扩展,使用 root 的子路径时无法 return 图像路径
Flask extension, failing to return image path when using sub-path of root
我正在努力为 Flask-Resize 添加额外的功能,特别是添加一个应该服务于原始文件的功能,而不是在大小和其他参数与原始文件相同的情况下生成缓存文件。
我已经在检查等方面完成了所有工作,如果图像在 RESIZE_ROOT
目录中,一切正常,但如果不在,当图像生成器检测到它不需要做任何东西和 returns 原始文件路径,jinja2 似乎无法获取图像。
在 RESIZE_ROOT
目录中使用大小为 200x300px 的图像 test_img.jpg
效果很好:
<img src="{{ 'test_img.jpg'|resize('200') }}"></img>
输出:
http://127.0.0.1:5000/static/images/test_img.jpg
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET /static/css/main.css HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET /static/images/test_img.jpg HTTP/1.1" 200 -
-- test_img.jpg fetched and displayed correctly
但是当图像被移动到子目录时说 ad/test_img.jpg
,然后控制台输出甚至不表明它正在获取图像
http://127.0.0.1:5000/static/images/ad/test_img.jpg
127.0.0.1 - - [23/Oct/2015 03:58:42] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:58:42] "GET /static/css/main.css HTTP/1.1" 200 -
-- ad/test_img.jpg isn't even being fetched
输入 <a href="{ 'ad/test_img.jpg'|resize('200') }">image</a>
生成图像的有效 link,因此文件路径正确返回,所以我不知道发生了什么。
这是相关代码; generate_image
如果检测到不应生成图像,则会引发 exc.StopImageGeneration
异常。
if not os.path.exists(full_cache_path):
try:
generate_image(inpath=original_path, outpath=full_cache_path,
format=format, width=width, height=height,
bgcolor=bgcolor, upscale=upscale, fill=fill,
anchor=anchor, quality=quality,
progressive=progressive,
placeholder_reason=placeholder_reason,
force_cache=force_cache)
except exc.StopImageGeneration:
full_cache_url = unicode(resize_url+image_url)
print (full_cache_url)
return full_cache_url.replace('\', '/')
如果重要的话还有我的 Flask-Resize 初始化参数:
(RESIZE_URL='http://127.0.0.1:5000/static/images/', RESIZE_ROOT='static/images/')
原来这与Flask/Jinja2无关,是Adblock的错;它默默地挡住了图像。
我为 localhost 和 127.0.0.1 添加了一个例外后就没有问题了。
我正在努力为 Flask-Resize 添加额外的功能,特别是添加一个应该服务于原始文件的功能,而不是在大小和其他参数与原始文件相同的情况下生成缓存文件。
我已经在检查等方面完成了所有工作,如果图像在 RESIZE_ROOT
目录中,一切正常,但如果不在,当图像生成器检测到它不需要做任何东西和 returns 原始文件路径,jinja2 似乎无法获取图像。
在 RESIZE_ROOT
目录中使用大小为 200x300px 的图像 test_img.jpg
效果很好:
<img src="{{ 'test_img.jpg'|resize('200') }}"></img>
输出:
http://127.0.0.1:5000/static/images/test_img.jpg
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET /static/css/main.css HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET /static/images/test_img.jpg HTTP/1.1" 200 -
-- test_img.jpg fetched and displayed correctly
但是当图像被移动到子目录时说 ad/test_img.jpg
,然后控制台输出甚至不表明它正在获取图像
http://127.0.0.1:5000/static/images/ad/test_img.jpg
127.0.0.1 - - [23/Oct/2015 03:58:42] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:58:42] "GET /static/css/main.css HTTP/1.1" 200 -
-- ad/test_img.jpg isn't even being fetched
输入 <a href="{ 'ad/test_img.jpg'|resize('200') }">image</a>
生成图像的有效 link,因此文件路径正确返回,所以我不知道发生了什么。
这是相关代码; generate_image
如果检测到不应生成图像,则会引发 exc.StopImageGeneration
异常。
if not os.path.exists(full_cache_path):
try:
generate_image(inpath=original_path, outpath=full_cache_path,
format=format, width=width, height=height,
bgcolor=bgcolor, upscale=upscale, fill=fill,
anchor=anchor, quality=quality,
progressive=progressive,
placeholder_reason=placeholder_reason,
force_cache=force_cache)
except exc.StopImageGeneration:
full_cache_url = unicode(resize_url+image_url)
print (full_cache_url)
return full_cache_url.replace('\', '/')
如果重要的话还有我的 Flask-Resize 初始化参数:
(RESIZE_URL='http://127.0.0.1:5000/static/images/', RESIZE_ROOT='static/images/')
原来这与Flask/Jinja2无关,是Adblock的错;它默默地挡住了图像。
我为 localhost 和 127.0.0.1 添加了一个例外后就没有问题了。