我的用戶可以上傳自己的圖像並將其用作頭像。現在,我正在努力如何檢索默認的後備圖像,如果他們自己沒有上傳圖像。使用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