Django FileField路径未正确返回

时间:2012-03-13 07:18:24

标签: python django heroku

我的django应用程序涉及将许多数据文件写入服务器,我的用户模型类的一部分看起来像这样

class UserData(models.Model):
    email = models.EmailField()
    course_id = models.IntegerField()
    data = models.FileField(upload_to=make_user_data_path)

def make_user_data_path(instance, filename):
    username, domain = instance.email.split('@')
    name = username + '.bin'
    return os.path.normpath(os.path.join('gameservice', 'userdata', domain, name))

在我的本地服务器上运行时,我在app / uploads / gameservice / userdata / sgi-singapore /中将文件保存为dummy_1.bin,dummy_2.bin,dummy_3.bin ...等,它工作正常

然而,当我部署到heroku时,它似乎无法检索数据..我得到这样的错误

media root = / app / uploads /

2012-03-13T05:15:28+00:00 app[web.1]: Traceback (most recent call last):
2012-03-13T05:15:28+00:00 app[web.1]: ERROR 2012-03-13 05:15:28,332 base 7 140089546524416 Internal Server Error: /gameservice/f1440caf4ae6d02b26c2387d14769af9/1/user_data
2012-03-13T05:15:28+00:00 app[web.1]:     response = callback(request, *callback_args, **callback_kwargs)
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
2012-03-13T05:15:28+00:00 app[web.1]:     return func(request, *args, **kwargs)
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner
2012-03-13T05:15:28+00:00 app[web.1]:     data = user_data.data.read()
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/core/files/utils.py", line 16, in <lambda>
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/core/files/storage.py", line 33, in open
2012-03-13T05:15:28+00:00 app[web.1]:     read = property(lambda self: self.file.read)
2012-03-13T05:15:28+00:00 app[web.1]:     return view_func(*args, **kwargs)
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/platformsite/gameservice/views.py", line 329, in user_data
2012-03-13T05:15:28+00:00 app[web.1]:     self._file = self.storage.open(self.name, 'rb')
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/db/models/fields/files.py", line 46, in _get_file
2012-03-13T05:15:28+00:00 app[web.1]:     return self._open(name, mode)
2012-03-13T05:15:28+00:00 app[web.1]:   File "/app/lib/python2.7/site-packages/django/core/files/storage.py", line 156, in _open
2012-03-13T05:15:28+00:00 app[web.1]:     return File(open(self.path(name), mode))
2012-03-13T05:15:28+00:00 app[web.1]: IOError: [Errno 2] No such file or directory: u'/app/uploads/gameservice/userdata/sgi-singapore.com/krislee_6547.bin'

当我输出保存时,似乎是正确的,

2012-03-13T05:06:42+00:00 app[web.1]: ERROR 2012-03-13 05:06:42,263 views 7 140236532705024 userdata post
2012-03-13T05:06:42+00:00 app[web.1]: ERROR 2012-03-13 05:06:42,267 models 7 140236532705024 path saved = gameservice/userdata/sgi-singapore.com/krislee.bin
2012-03-13T05:06:42+00:00 heroku[router]: POST pure-fire-3271.herokuapp.com/gameservice/4ed1c492de837e0cc41596378ed0fb9f/1/user_data dyno=web.1 queue=0 wait=0ms service=451ms status=200 bytes=5
2012-03-13T05:06:43+00:00 app[web.1]: ERROR 2012-03-13 05:06:43,121 views 10 140236532705024 userdata post
2012-03-13T05:06:43+00:00 app[web.1]: ERROR 2012-03-13 05:06:43,127 models 10 140236532705024 path saved = gameservice/userdata/sgi-singapore.com/krislee.bin
2012-03-13T05:06:43+00:00 heroku[router]: POST pure-fire-3271.herokuapp.com/gameservice/4ed1c492de837e0cc41596378ed0fb9f/1/user_data dyno=web.1 queue=0 wait=0ms service=379ms status=200 bytes=5
2012-03-13T05:06:43+00:00 app[web.1]: ERROR 2012-03-13 05:06:43,826 views 8 140236532705024 userdata post
2012-03-13T05:06:43+00:00 app[web.1]: ERROR 2012-03-13 05:06:43,832 models 8 140236532705024 path saved = gameservice/userdata/sgi-singapore.com/krislee.bin
2012-03-13T05:06:44+00:00 heroku[router]: POST pure-fire-3271.herokuapp.com/gameservice/4ed1c492de837e0cc41596378ed0fb9f/1/user_data dyno=web.1 queue=0 wait=0ms service=381ms status=200 bytes=5
2012-03-13T05:06:44+00:00 app[web.1]: ERROR 2012-03-13 05:06:44,557 views 10 140236532705024 userdata post
2012-03-13T05:06:44+00:00 app[web.1]: ERROR 2012-03-13 05:06:44,562 models 10 140236532705024 path saved = gameservice/userdata/sgi-singapore.com/krislee.bin
2012-03-13T05:06:44+00:00 heroku[router]: POST pure-fire-3271.herokuapp.com/gameservice/4ed1c492de837e0cc41596378ed0fb9f/1/user_data dyno=web.1 queue=0 wait=0ms service=363ms status=200 bytes=5
2012-03-13T05:06:45+00:00 app[web.1]: ERROR 2012-03-13 05:06:45,263 views 10 140236532705024 userdata post
2012-03-13T05:06:45+00:00 app[web.1]: ERROR 2012-03-13 05:06:45,268 models 10 140236532705024 path saved = gameservice/userdata/sgi-singapore.com/krislee.bin
2012-03-13T05:06:45+00:00 heroku[router]: POST pure-fire-3271.herokuapp.com/gameservice/4ed1c492de837e0cc41596378ed0fb9f/1/user_data dyno=web.1 queue=0 wait=0ms service=367ms status=200 bytes=5
2012-03-13T05:06:46+00:00 heroku[router]: POST pure-fire-3271.herokuapp.com/gameservice/4ed1c492de837e0cc41596378ed0fb9f/1/save_score dyno=web.1 queue=0 wait=0ms service=154ms status=200 bytes=14

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我上传的文件竟然是json文件,我最终将它们切换到文本字段并将这些文件上传到数据库而不是上传的目录文件。这完全解决了我的问题。如果有人有兴趣