m0_59759913 2021-07-28 15:44 采纳率: 72.2%
浏览 754
已结题

这个问题怎么解决,为什么会报错?

我正在学习python crash course, 2nd edition 第18章。做到激活模型需要makemigrations.

from django.db import models


# Create your models here.

class Topic(models.Model):
    """A topic the user is learning about."""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True) 

    def __str__(self):
        """Return a string representation of the model."""
        return self.text`

然后报错:
Traceback (most recent call last):
File "C:\Users\Dell-PC\learning_logs\models.py", line 6, in
class Topic(models.Model):
File "C:\Users\Dell-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 108, in new
app_config = apps.get_containing_app_config(module)
File "C:\Users\Dell-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\Dell-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "C:\Users\Dell-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\django\conf_init_.py", line 82, in getattr
self.setup(name)
File "C:\Users\Dell-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\django\conf_init
.py", line 63, in _setup
raise ImproperlyConfigured(
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.
[Finished in 383ms]

我用的是sublime text.
怎么解决?

  • 写回答

5条回答 默认 最新

  • 一眼青苔 2021-07-28 17:36
    关注

    官网说明

    地址:

    https://docs.djangoproject.com/en/1.11/topics/settings/#designating-the-settings

    解释:

    文章大意是在使用 Django 时,必须告诉它你正在使用哪些设置。这些可以通过环境变量 DJANGO_SETTINGS_MODULE设置,程序在执行的时候会自动使用或者你也可以在访问设置之前调用 settings.configure()。

    结论:

    由于你没有设置环境变量,导致你的程序在执行时未找到相应的配置,因此报错了,提醒你应该进行配置。

    补充:

    DJANGO_SETTINGS_MODULE 的值应该是 Python 路径语法,例如Mysite.settings。请注意,设置模块应位于 Python 导入搜索路径上。

    希望对你有所帮助。

    评论

报告相同问题?

问题事件

  • 系统已结题 8月5日
  • 创建了问题 7月28日

悬赏问题

  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理