2017-03-09 60 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