无法将模型导入到 DJANGO 视图中

发布于 2025-01-09 09:37:54 字数 1014 浏览 1 评论 0原文

这里是 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 :

project structure

webpage folder structure

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2025-01-16 09:37:54

请检查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')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文