【发布时间】:2018-12-12 09:02:12
【问题描述】:
我目前正在学习如何使用 Django 开发 Web 服务。当我学习关于 Udemy 的在线课程时,我在 models.py 中使用 ImageField 时遇到了问题。我的问题是,当我执行 runserver 命令时,Django 显示以下错误,尽管我很确定库 Pillow 已安装。
在models.py中,我有以下代码:
from django.db import models
from django.contrib.auth.models import User
class UserProfileInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT)
portfolio = models.URLField(blank=True)
picture = models.ImageField(upload_to="profile_pics")
def __str__(self):
return user.self.username
当我运行服务器时,我收到以下错误: python manage.py 运行服务器 正在执行系统检查...
Unhandled exception in thread started by <function check_errors<locals>.wrapper at 0x10407b6a8>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/core/management/base.py", line 410, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check
identified some issues:
ERRORS:
first_app.UserProfileInfo.picture: (fields.E210) Cannot use ImageField because
Pillow is not installed.
HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip
install Pillow".
System check identified 1 issue (0 silenced).
因此,按照消息,我使用此处介绍的方法检查了已安装的模块:How can I get a list of locally installed Python modules?。以下代码在与 manage.py 相同目录中的文件中运行。我正在使用 Pycharm 来使用虚拟环境,所以我假设我使用的环境与运行服务器时 Django 项目使用的环境相同。
通过上述方法,我可以看到 Pillow 已安装,如下所示:
['argon2==0.1.10', 'bcrypt==3.1.4', 'cffi==1.11.5', 'django==2.0.5', 'faker==0.8.15', ' olefile==0.45.1'、'pillow==5.0.0'、'pip==9.0.1'、'pycparser==2.18'、'python-dateutil==2.7.3'、'pytz==2018.4' , 'setuptools==28.8.0', 'six==1.11.0', 'text-unidecode==1.2']
我该如何解决这个问题?或者,有没有其他方法可以在没有 Pillow 的情况下在 Django 中使用图像?
【问题讨论】:
-
你确定你在那个特定的 python 安装上运行 django 吗?
-
你是从 pycharm 安装 Pillow 的吗?
-
你在使用 virtualenv 吗?如果是,请使用“pip freeze”命令以显示已安装的 virtualenv 模块。也许你在你的机器上安装了枕头,而不是在 virtualenv 上
-
@MohammadAli 我在与我的 Django 项目相同的目录中运行 pip freeze 并看到 Pillow 已安装,尽管它的版本比我打算使用的版本旧。我的问题是,这些库是我的 Django 项目使用的库吗?如果是,我该如何操作 Django 项目的库?我一直假设通过 PyCharm 导入和导出库会改变我项目的库。还有,PyCharm中设置为项目解释器的解释器是不是我的Django项目使用的解释器?
-
@icyfire 是的,我通过 PyCharm 安装了 Pillow。我去了首选项,项目解释器,然后通过单击“+”按钮安装 Pillow。这是向我的 Django 项目添加库的正确方法吗?
标签: python django django-models pillow