无法将模型导入到 DJANGO 视图中
这里是 Django 的新手。
我正在尝试将模型导入视图中以使用它。然而我这样做遇到了问题。
首先,这是我的文件夹的结构:
当我尝试将我的模型之一导入视图时:
from .models import model_i_want
我得到:
ImportError: attempted relative import with no known parent package
如果我尝试,
from webpage.models import model_i_want
我得到:
ModuleNotFoundError: No module named 'webpage'
如果我尝试要导入这样的模型,
import .models
我收到此错误
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.
我的 webapp 已添加到设置中,并且在遵循 Django 官方教程时一切都运行顺利。我知道我应该阅读文档并弄清楚它,但我似乎还不能理解所有内容,而且我仍在努力适应 Django。
谢谢您,祝您有愉快的一天!
Newbie to Django here.
I am trying to import a model into views to use it. However I am facing problems doing so.
First of all, here is the structure of my folder :
When I try to import one of my models into views with:
from .models import model_i_want
I get :
ImportError: attempted relative import with no known parent package
if I try
from webpage.models import model_i_want
I get :
ModuleNotFoundError: No module named 'webpage'
if I try to import models like this
import .models
I get this error
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.
My webapp is added in the settings, and everything was running smoothly while following the Django official tutorial. I know I should be reading the documentation and figure it out but I can't seem to understand all of it yet and I am still trying to get used to Django.
Thank you and have a nice day!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请检查manage.py文件并设置以下内容
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '设置文件路径')
Pls check the manage.py file and set the following
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'path to settings file')