python - ImageField 未定义

标签 python django imagefield

我正在使用 Django 构建个人摄影网站。我正在处理 models.py。我的应用名为“photoViewer”,网站名为“photoSite”。但是,当我运行 makemigrations 时,我得到:

NameError: 名称“ImageField”未定义

我在本地 "photoSite\photoViewer\static\photoViewer\images" 中存储了 3 张图像,我想在完成的网站上显示它们。

模型.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

设置.py:

...
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '..', 'static').replace('\\','/')
STATIC_URL = '/static/'
...

最佳答案

看起来你有一个语法问题......

photo_img = ImageField(upload_to = "images/")

应该是……

photo_img = models.ImageField(upload_to = "images/")

关于python - ImageField 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003371/

相关文章:

django - 将 json 模型字段与 django graphene 一起使用

python - 替换 Django 图像不会删除原始图像

python - 通过 Django Admin 将图像上传到 ImageField 时出错

python - 任何 Python ORM 都充分利用了 NoSQL 的无模式功能吗?

python - 我正在尝试准确了解如何使用 cmd 模块

python - django 将日期字段更改为整数字段无法迁移

python - django安装问题[python]

python - Django ImageField 不会在网页上显示

python - 如何将python list [a,b,c,d]转换为[a:b,c:d]格式

python - 如何在 python pandas 中使用带有 bool 的查询函数?