首页 > 解决方案 > Django文件从shell运行但不运行但自己运行

问题描述

尝试在我的项目中运行文件时,出现错误:

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

但是,当运行python3 manage.py shell代码并将其粘贴到 shell 中时,它可以正常工作。

该文件如下所示:

from models import *

products = Product.objects.all()
iterable_form = []
for p in products:
    a = []
    a.append(p.ID)
    a.append(p.title)
    a.append(p.official_path_name)
    a.append(p.quantity)
    a.append(p.language)
    a.append(p.book_type)
    a.append(p.authorID)
    a.append(p.editionID)
    iterable_form.append(a)

print(iterable_form)

标签: pythondjangoshell

解决方案


推荐阅读