2017-03-09 61 views
1

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

任何想法?

+0

你檢查過'path'變量的值嗎? –

+0

是的。它解決了通往正確路徑的途徑。我更新了上面的問題,以反映這 – ThomasD

回答

0

如果你聲明的文件本身作爲一個static_file或它的目錄或文件路徑的任何目錄static_dir您的應用程序/服務的.yaml配置文件裏,然後,默認情況下,它不是應用程序代碼訪問。您需要將其配置爲application_readable。 From Handlers element

application_readable

Optional. Boolean. By default, files declared in static file handlers are uploaded as static data and are only served to end users. They cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas .

+0

是否有任何缺陷,以啓用設置application_readable? – ThomasD

+0

AFAIK在文件配額中統計它們兩次,並在兩次上傳時延長部署時間。 –

+0

謝謝。你知道我是否可以將application_readable放在子目錄中嗎?例如。將application_readable放在/ static/images但不適用於/ static中的其他目錄和文件? – ThomasD