【发布时间】: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,否则我无法与新代码交互。有没有可能避免它?当我重新导入时,不考虑类更改。