我正在使用Django构建个人摄影网站。我正在研究models.py。我的应用程序名为“photoViewer”,该网站名为“photoSite”。但是,当我运行makemigrations时,我得到了:
NameError: name 'ImageField' is not defined
我在本地"photoSite\photoViewer\static\photoViewer\images"
存储了3张图片,我想在完成的网站上显示。
Models.py:
from django.db import models
# Create your models here.
class Photo (models.Model):
photo_title = models.CharField(max_length=200)
photo_img = ImageField(upload_to = "images/") #Error here
def __str__(self): # __unicode__ on Python 2
return self.photo_title
class Album (models.Model):
photos= models.ManyToManyField(Photo)
album_title = models.CharField(max_length=200)
def __str__(self): # __unicode__ on Python 2
return self.album_title
settings.py:
...
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '..', 'static').replace('\\','/')
STATIC_URL = '/static/'
...
答案 0 :(得分:5)
看起来你有语法问题......
photo_img = ImageField(upload_to = "images/")
应该......
photo_img = models.ImageField(upload_to = "images/")