当我尝试将视图导入django应用程序的apps.py模块时,出现此错误。我正在使用django 2.2.9,python 3.7.6,django-oscar 2.0.4。我已经覆盖了checkout应用程序,但是此错误使我无法为django oscar的所有应用程序添加视图或更改URL。是什么导致此错误?
(vsosci) c:\Users\anon\Documents\sosci.git>python manage.py check
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\anon\Envs\vsosci\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "c:\Users\anon\Documents\sosci.git\checkout\apps.py", line 3, in <module>
from checkout.views import PaymentDetailsView
File "c:\Users\anon\Documents\sosci.git\checkout\views.py", line 1, in <module>
from oscar.apps.checkout.views import PaymentDetailsView
File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\apps\checkout\views.py", line 17, in <module>
= get_classes('checkout.forms', ['ShippingAddressForm', 'ShippingMethodForm', 'GatewayForm'])
File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\core\loading.py", line 41, in get_classes
return class_loader(module_label, classnames, module_prefix)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\core\loading.py", line 99, in default_class_loader
oscar_module = _import_module(oscar_module_label, classnames)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\core\loading.py", line 133, in _import_module
return __import__(module_label, fromlist=classnames)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\apps\checkout\forms.py", line 2, in <module>
from django.contrib.auth.forms import AuthenticationForm
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\contrib\auth\forms.py", line 10, in <module>
from django.contrib.auth.models import User
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\db\models\base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\anon\Envs\vsosci\lib\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.
下面的代码块中的第三行将引发错误
import oscar.apps.checkout.apps as apps
from django.urls import re_path
from checkout.views import PaymentDetailsView
class CheckoutConfig(apps.CheckoutConfig):
name = 'checkout'
def ready(self):
super().ready()
self.paymentdetailsview = PaymentDetailsView
def get_urls(self):
urls = super().get_urls()
urls += [
re_path(r'payment-details/(?P<course_id>\d+)/$',
self.paymentdetailsview.as_view(), name='payment-details'),
re_path(r'payment-details/$',
self.paymentdetailsview.as_view(), name='oscar-payment-details'),
re_path(r'thank-you/$', self.thankyou_view.as_view(),
name='thank-you'),
]
return self.post_process_urls(urls)
包含可解决我的问题的修复程序的修改
import oscar.apps.checkout.apps as apps
from django.urls import re_path
class CheckoutConfig(apps.CheckoutConfig):
name = 'checkout'
def ready(self):
super().ready()
from checkout import views as checkout_views
self.checkout_views = checkout_views
def get_urls(self):
urls = super().get_urls()
urls += [
re_path(r'payment-details/(?P<course_id>\d+)/$',
self.checkout_views.PaymentDetailsView.as_view(), name='payment-details'),
re_path(r'payment-details/$',
self.checkout_views.PaymentDetailsView.as_view(), name='oscar-payment-details'),
re_path(r'thank-you/$', self.thankyou_view.as_view(),
name='thank-you'),
]
return self.post_process_urls(urls)
答案 0 :(得分:0)
从docs
尽管您无法在定义AppConfig类的模块级别上导入模型,但是可以使用import语句或get_model()将它们导入ready()中。
通过导入PaymentDetailsView
,您正在导入模型,因为checkout.views
大概是在导入一些