我有一个项目可以创建我想用 uuid 键入的对象。当我在 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,它是一个空白字符串。