【问题标题】:Apps aren't loaded yet while doing the official Django tutorial执行官方 Django 教程时尚未加载应用程序
【发布时间】:2020-05-02 04:37:18
【问题描述】:

我在执行official Django tutorial 时遇到了 Django 设置问题。当我对文件 models.py 执行调试模式 (F11) 时,PyDev 控制台返回错误 Apps aren't loaded yet。这是一个非常简单的项目,我只将项目名称更改为“capital”,将应用程序名称更改为“marketsdata”。

这个类似问题中提出的解决方法不能解决我的问题。 Django 1.7 upgrade error: AppRegistryNotReady: Apps aren't loaded yet

官方教程怎么可能在基本的 settings.py 和 models.py 文件中返回这样的错误?我做错了什么?

models.py

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

settings.py

INSTALLED_APPS = [
    'marketsdata.apps.MarketsdataConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

apps.py

from django.apps import AppConfig


class MarketsdataConfig(AppConfig):
    name = 'marketsdata'

urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('marketsdata/', include('marketsdata.urls')),
    path('admin/', admin.site.urls),
]

调试器输出:

pydev debugger: starting (pid: 23504)
Traceback (most recent call last):
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 3129, in <module>
    main()
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 3122, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 2195, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/pydevd.py", line 2202, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/abc/.eclipse/360744294_linux_gtk_x86_64/plugins/org.python.pydev.core_7.5.0.202001101138/pysrc/_pydev_imps/_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/abc/python/capital/marketsdata/models.py", line 3, in <module>
    class Question(models.Model):
  File "/home/abc/.local/lib/python3.6/site-packages/django/db/models/base.py", line 107, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/abc/.local/lib/python3.6/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/home/abc/.local/lib/python3.6/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

【问题讨论】:

  • 您没有“运行”models.py 文件。您使用 python3 manage.py runserver 运行 Django 服务器。
  • @WillemVanOnsem 你不能用 Django 与控制台中的代码进行交互吗?
  • 是的,python3 manage.py shell
  • @WillemVanOnsem 和 python3 manage.py shell 当我更改我的课程时,我需要重新启动 shell,否则我无法与新代码交互。有没有可能避免它?当我重新导入时,不考虑类更改。

标签: python django pydev


【解决方案1】:

要让 PyDev 运行 django 项目,您必须右键单击该项目并执行以下操作:

Run as &gt; PyDev: Django

在后台,这将执行一个调用 python manage.py runserver 的启动(您也可以创建一个自定义启动来执行此操作,PyDev 只是让它更容易一些)。

如果你在处理 Django 时想要一个 shell,你必须右键单击项目并选择 Django &gt; Shell with Django environment(这将为你提供一个 django shell,但支持 PyDev 的代码完成)。

请注意,代码不会自动添加到该外壳...如果您更改代码,您需要重新启动外壳或手动reload您想要的模块(但鉴于 django 有很多魔力可能很难重新加载)。

注意:您可能需要查看http://www.pydev.org/manual_adv_django.html,了解有关如何在 PyDev 中开发 Django 的更多详细信息。

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 2011-09-07
    • 2019-05-10
    • 1970-01-01
    • 2016-08-04
    • 2016-06-01
    • 2016-01-08
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多