Django NameError:未定义名称'bPath' [英] Django NameError: name 'bPath' is not defined
问题描述
我在Django 1.7上运行,当我运行python manage.py migrate时,我收到以下错误
I'm running on Django 1.7 and when I run python manage.py migrate I get the following error
File "/home/ymorin007/workspace/sites/jantiyes.com/src/deeds/migrations/0006_auto_20141204_1631.py", line 9, in <module>
class Migration(migrations.Migration):
File "/home/ymorin007/workspace/sites/jantiyes.com/src/deeds/migrations/0006_auto_20141204_1631.py", line 19, in Migration
field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(location=bPath('/home/ymorin007/workspace/sites/jantiyes.com/src/media')), max_length=255, null=True, upload_to=deeds.models.picture_name, blank=True),
NameError: name 'bPath' is not defined
这是我的行为/ models.py
This is my deeds/models.py
from jantiyes.settings.base import MEDIA_ROOT
upload_storage = FileSystemStorage(location=MEDIA_ROOT)
def picture_name(self, filename):
ext = filename.split('.')[-1]
deedname = re.sub('[ ]', '-', self.text.lower())
filename = "DEED-%s-%s.%s" % (self.id, deedname, ext)
url = "%s" % filename
return url
class Deed(TimeStampedModel):
picture = models.ImageField(upload_to=picture_name, null=True, blank=True, storage=upload_storage, max_length=255)
text = models.CharField(max_length=500)
when = models.DateField(unique=True)
我的媒体声明:
BASE_DIR = Path(__file__).ancestor(3)
MEDIA_ROOT = BASE_DIR.child("media")
推荐答案
如果没有的确切定义, jantiyes.settings.base.MEDIA_ROOT
但我想这是一个类的一个实例( bPath
),不是 解构 ,那是一个子类 unicode
。因此,迁移作者假定它不需要任何导入,并且简单地 repr
该值被证明是 bPath('/ home / ymorin007 / workspace / / c $ c $。
It's hard to tell without the exact definition of jantiyes.settings.base.MEDIA_ROOT
but I guess it's an instance of a class (bPath
) that is not deconstructible and that is a subclass of unicode
. Hence the migration writer assumes it doesn't need any imports and simply repr
the value which turns out to be bPath('/home/ymorin007/workspace/sites/jantiyes.com/src/media')
.
您有两个选择:
- 确保
jantiyes.settings.base.MEDIA_ROOT
被定义为字符串,从而由迁移作者正确处理。例如MEDIA_ROOT ='/home/ymorin007/workspace/sites/jantiyes.com/src/media'
在您的jantiyes.settings.base
模块文件。 - 通过定义一个<$ c>确保
bPath
类是可解构 $ c> deconstruct 方法返回导入路径到自己。
- Make sure
jantiyes.settings.base.MEDIA_ROOT
is defined as string and thus correctly handled by the migration writer. e.g.MEDIA_ROOT = '/home/ymorin007/workspace/sites/jantiyes.com/src/media'
in yourjantiyes.settings.base
module file. - Make sure the
bPath
class is deconstructible by defining adeconstruct
method returning the import path to itself.
这篇关于Django NameError:未定义名称'bPath'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!