【发布时间】:2017-03-09 09:15:24
【问题描述】:
我的用户可以上传自己的图片并将其用作头像。现在,如果他们自己没有上传图片,我正在努力如何检索默认的后备图片。
头像的路径是“//mysite.com/avatar/username”。
到目前为止,我有这段代码,当用户自己上传头像时它可以正常工作,但是当我尝试检索默认图像时它给了我以下错误:
引发 IOError(errno.EACCES, '文件不可访问', 文件名)
IOError:[Errno 13] 文件不可访问:'/Users/myuser/Documents/github/mysite/static/images/profile.png'
def get(self):
path = self.request.path.split('/')
action = self.get_action(path)
if action:
e = employees.filter('username = ', action).get()
if e.avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(e.avatar)
else:
self.response.headers['Content-Type'] = 'image/png'
path = os.path.join(os.path.split(__file__)[0], 'static/images/profile.png')
with open(path, 'r') as f:
print self.response.out.write(f.read())
我已在 app.yaml 中将“/static”文件夹定义为 static_dir。
我知道我可以将 profile.png 放在根文件夹中,但我更喜欢将它放在“/static/images”文件夹中。
有什么想法吗?
【问题讨论】:
-
你检查
path变量的值了吗? -
是的。它将路径解析为正确的路径。我更新了上面的问题以反映这一点