在执行官方 Django 教程时,应用程序尚未加载
Apps aren't loaded yet while doing the official Django tutorial
我在执行 official Django tutorial 时遇到 Django 设置问题。当我在文件 models.py 上执行调试模式 (F11) 时,PyDev 控制台 returns 出现错误 Apps aren't loaded yet
。这是一个非常简单的项目,我只将项目名称更改为 'capital' 并将应用程序名称更改为 'marketsdata'.
这个类似问题中提出的解决方法没有解决我的问题。
Django 1.7 upgrade error: AppRegistryNotReady: Apps aren't loaded yet
官方教程returns怎么可能出现基础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.
对于 PyDev 到 运行 django 项目,您必须右键单击该项目并执行以下操作:
Run as > PyDev: Django
在引擎盖下,这将执行一个调用 python manage.py runserver
的启动(您也可以创建一个自定义启动来执行此操作,PyDev 只是让它更容易一些)。
如果你在处理 Django 时想要一个 shell,你必须右键单击该项目,然后 select Django > Shell with Django environment
(这会给你一个 django shell但支持 PyDev 的代码完成)。
请注意,代码不会自动添加到 shell...如果您更改代码,则需要重新启动 shell 或手动 reload
您想要的模块(但是鉴于 django 有很多魔力,可能很难重新加载)。
注意:您可能想查看 http://www.pydev.org/manual_adv_django.html 以了解有关如何在 PyDev 中开发 Django 的更多详细信息。
我在执行 official Django tutorial 时遇到 Django 设置问题。当我在文件 models.py 上执行调试模式 (F11) 时,PyDev 控制台 returns 出现错误 Apps aren't loaded yet
。这是一个非常简单的项目,我只将项目名称更改为 'capital' 并将应用程序名称更改为 'marketsdata'.
这个类似问题中提出的解决方法没有解决我的问题。 Django 1.7 upgrade error: AppRegistryNotReady: Apps aren't loaded yet
官方教程returns怎么可能出现基础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.
对于 PyDev 到 运行 django 项目,您必须右键单击该项目并执行以下操作:
Run as > PyDev: Django
在引擎盖下,这将执行一个调用 python manage.py runserver
的启动(您也可以创建一个自定义启动来执行此操作,PyDev 只是让它更容易一些)。
如果你在处理 Django 时想要一个 shell,你必须右键单击该项目,然后 select Django > Shell with Django environment
(这会给你一个 django shell但支持 PyDev 的代码完成)。
请注意,代码不会自动添加到 shell...如果您更改代码,则需要重新启动 shell 或手动 reload
您想要的模块(但是鉴于 django 有很多魔力,可能很难重新加载)。
注意:您可能想查看 http://www.pydev.org/manual_adv_django.html 以了解有关如何在 PyDev 中开发 Django 的更多详细信息。