django.core.exceptions.ImprperlyConfigured 解决方案
我想知道为什么会收到此错误:
django.core.exceptions.ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置。在访问设置之前,您必须定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。
这是我的代码:
from main_app.models import student
std1 = student(name='tom', fullname='scholz')
std1.save()
print(student.objects.all())
运行服务器和迁移没有任何问题。但是当运行 .py 文件(我自己制作的)时,就会出现这样的错误。
I wonder why I get this error :
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.
this is my code :
from main_app.models import student
std1 = student(name='tom', fullname='scholz')
std1.save()
print(student.objects.all())
there is not any problem with running server and migrations. but when it comes to running a .py file (made by myself) such an error rises.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题只是因为 Django 不知道在哪里找到所需的设置。
正如[文档][1]提到的:
根据文档,您应该通过定义环境变量来做到这一点(通知 Django 有关设置)。
据我所知,面对这个错误该怎么做几乎没有什么含糊之处,提供了您可以采取的不同解决方案的列表:
1-在操作系统中定义一个环境变量([链接][2])。
2- 如果您使用的是虚拟环境; 以这种方式在 activate.bat 中设置一个新的环境变量
,每当您激活虚拟环境时,都会自动定义该变量。
3-(就像你所做的那样!)在每个 python 文件中;编写定义该环境变量的命令。
但是如果您使用第三种方法,那么在哪里放置该命令非常重要(在第二种情况下,而且绝对是第一种情况下,我们根本不在乎!)。
所以你应该注意不要在脚本中的该命令之前使用 Django。
(有些人想知道为什么使用这个命令后仍然出现同样的错误。原因是他们把这行代码放在以前使用过Django的地方)
你可能会问为什么当你使用迁移等等时;你没有遇到这个错误。如果我们看一下manage.py,原因就会很清楚
因为那里有那个命令(上面列表的第三种方法)
但是你
[1]: https://docs.djangoproject.com /en/4.0/topics/settings/#designating-the-settings
[2]: https://www.computerhope.com/issues/ch000549.htm
This problem is simply because Django doesn't know where to find required settings.
As it's [Documentation][1] mentions :
based on Documentation you should do that (informing Django about the setting)by defining a environmental variable.
as I see that there are little ambiguities about what to do facing this error a list of different solutions you can take, is provided :
1- define a environment variable in the operating system([link][2]).
2- if you are using a virtual environment; set a new environment variable in activate.bat
in this way, whenever you activate the virtual environment that variable would be defined automatically.
3- (the way you did!)in each python file; write the command which defines that environment variable.
but if you use the third method, it is really important where you are putting that command(in the second and definitely first case we don't care at all!).
so you should take care of not using Django before that command in your script.
(some people wonder why after using this command still we are getting the same error. the reason is they put that line of code in a place where Django was used before)
you might ask why when you were using migration and so on; you didn't face this error. the reason would be clear if we take a look at manage.py
because there is that command there(third method of the above list)
but you
[1]: https://docs.djangoproject.com/en/4.0/topics/settings/#designating-the-settings
[2]: https://www.computerhope.com/issues/ch000549.htm
对我来说,当我尝试在项目中使用 Pytest 时出现了这个问题。我在 pytest.ini 中定义了“DJANGO_SETTINGS_MODULE”,但问题仍然存在。所以我卸载了 pytest-django 并重新安装它,它起作用了。你也可以尝试一下。它可能会起作用。
卸载步骤:-
重新安装步骤:-
希望它可以解决您的问题。当你的代码没有任何问题,但由于某些技术原因仍然出现错误,而且很难发现错误时,这会令人沮丧。
For me the issue raised when I was trying to use Pytest in my project. I defined the 'DJANGO_SETTINGS_MODULE' in pytest.ini still the issue was there. So I uninstalled the pytest-django and reinstalled it, and it worked. You can try the same. It might Work.
Steps to uninstall :-
Steps to Re-install :-
Hope that it might resolve your problem. Its frustrating when there is nothing wrong in your code, but you still get error for some technical reason, which is even hard to find.
使用 Django 时,可以使用 Django 应用程序 shell 导入与项目相关的所有变量。
在终端中:
您必须位于
manage.py
文件所在的目录。希望这会有所帮助。When you use Django, you can use the Django application shell that imports all the variables relatives to the project.
In a terminal:
You have to be on the directory where the
manage.py
file is. Hope this can be helpful.