【发布时间】:2018-02-12 21:57:22
【问题描述】:
我知道已经有人提出并回答了这个问题,但到目前为止我看到的解决方案并没有解决问题。我正在尝试在 python 中运行以下命令来验证登录我当前正在构建的网站的用户。
from django.contrib.auth.models import User
但是,我收到以下错误:
Traceback (most recent call last):
"C:\Users\<user>\Documents\GitHub\MegaPortal\BackEnd\BackEnd\test.py", line 4, in <module>
from django.contrib.auth.models import User
File "C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
奇怪的是,当我在 Atom 上运行时,这个命令会出错,但是当我使用 Django 的 shell 运行时,它不会出错。我已经尝试在我的 settings.py 文件中放入以下命令
import django
django.setup()
但还是什么都没有。
非常感谢任何帮助。
编辑:我从系统 python 而不是 virtualenv 运行代码。该代码尚未在 GitHub 上。 Django 版本是 2.0.2。我的代码是:
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
user = authenticate(username='john', password='secret')
if user is not None:
# redirect to personal page
else:
print(' ERROR pasword or username is not correct')
【问题讨论】:
-
你是从系统python而不是virtualenv运行它吗?该项目是否在 Github 上公开?你用的是什么版本的 django?
-
您的问题中没有包含足够的代码来重现该问题,因此很难提供帮助。在您的设置中输入
django.setup()是不正确的 - 您在哪里读到它会有所帮助? -
抱歉信息不足。这是我在这里的第一篇文章。我已编辑原始问题以回答您的问题。