在virtualenv中将Django项目切换到gunicorn服务器时出错

时间:2013-04-20 23:02:42

标签: django django-models virtualenv gunicorn

我有一个项目可以创建我想用uuids键入的对象。当我在manage.py中使用简单的Django测试服务器运行这个项目时,一切都很好。但是我正在尝试部署到heroku,所以我设置了一个带有gunicorn服务器的virtualenv,它破坏了我的代码。我已经跟踪了这个错误,因为在某些情况下,当使用此服务器运行时,我的UploadedFile对象始终具有空白访问链接。

以下是来自models.py的代码:

 from django.db import models
 import uuid
 import datetime

 # UUID field will be used to key file upload objects
 class UUIDField(models.CharField) :
   def __init__(self, *args, **kwargs):
     kwargs['max_length'] = kwargs.get('max_length', 64 )
     kwargs['blank'] = True
     models.CharField.__init__(self, *args, **kwargs)

 class UploadedFile(models.Model):
   #accessLink = UUIDField(primary_key=True, editable=False)
   accessLink = models.CharField(primary_key=True, max_length=64)
   uploadTime = models.DateTimeField(default=datetime.datetime.now)  
   filename = models.CharField(max_length=200)

   def __init__(self, *args, **kwargs):
     super(UploadedFile, self).__init__(*args, **kwargs)
     if self.accessLink is '':
       self.accessLink = str(uuid.uuid4())

   def __unicode__(self):
     return filename

以下是views.py中包含的索引的代码:

 from django.http import HttpResponse  # Just for lulz                                            
 from django.shortcuts import render
 from django.core.context_processors import csrf
 from django.shortcuts import render_to_response
 from django.template import Template, Context
 from smartfile import BasicClient
 from filemapper.models import UploadedFile

 def index(request):
   if request.method == 'POST':
     c = {}
     c.update(csrf(request))
     authKey = 'ggcCEFzGBcJYAQSHNf7AnF8r7c03cB'
     authPassword = 'CJlxZHCocieiPOKuhI6GdGOwwTMr2i'
     api = BasicClient(authKey, authPassword)
     for f in request.FILES.values():
       # Create our record of the file
       u = UploadedFile(filename=f.name) 
       u.save()
       # Create a directory on smartfile
       api.post('/path/oper/mkdir/', path=u.accessLink)
       # Upload the file to s
       api.post('/path/data/' + u.accessLink, file=f.file, name=u.filename)
       # This page should display how to access the uploade
       return generate(request, u.accessLink)
     else:
       return HttpResponse('File not found')
   else:
     return render(request, 'filemapper/index.html')

当我尝试将文件发布到索引时代码失败,因为u.accessLink不是正确形成的UUID,它是一个空字符串。

1 个答案:

答案 0 :(得分:0)

这里有一些事情,我作为Django开发人员会做另一种方式:

  • 无需对UUIDField进行编码,只需使用包含max_lengthblank=True
  • 的字段
  • 不要覆盖UploadedFile.__init__ - 在UploadedFile.save()方法
  • 中设置accessLink

此外,在重载新样式类方法时,您应该使用super,而不是显式调用__init__方法。