我的用户可以上传自己的图像并将其用作头像。现在,我正在努力如何检索默认的后备图像,如果他们自己没有上传图像。使用python和GAE检索默认的后备头像图像
头像的路径是“//mysite.com/avatar/username”。
到目前为止,我有这样的代码,当用户上传头像自己,但它给了我下面的错误,当我尝试检索默认图像的正常工作:
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/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'变量的值吗? –
是的。它解决了通往正确路径的途径。我更新了上面的问题,以反映这 – ThomasD