3

I know that this question had been asked and answered already, but the solutions I saw so far are not solving the problem. I am trying to run the following command in python to authenticate users who log in a website I am currently building.

from django.contrib.auth.models import User

However, I am getting the following error:

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.

What is weird is that this command gives an error when I run on Atom, but when I run using Django's shell, it does not give any error. I have already tried putting in my settings.py file the following command

import django
django.setup()

but still nothing.

Any help is highly appreciated.

EDIT: I am running the code from system python not a virtualenv. The code is not on GitHub yet. The Django version is 2.0.2. My code is:

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')
Jtou2
  • 31
  • 1
  • 4
  • Are you running that from system python rather than a virtualenv? Is that project public on Github? What version of django are you using? – markwalker_ Feb 12 '18 at 22:25
  • 1
    You haven't included enough code in your question to reproduce the issue so it's difficult to help. Putting `django.setup()` in your settings is incorrect - where did you read that it would help? – Alasdair Feb 12 '18 at 22:58
  • 1
    Sorry for the lack of information. That is my first post in here. I have edited the original question to answer your questions. – Jtou2 Feb 13 '18 at 00:04
  • https://stackoverflow.com/questions/39723310/django-standalone-script Think that will help – Alvin Feb 13 '18 at 02:53

1 Answers1

0

start cmd in your project's directory and type cd to main directory of project(where the "manage.py" file is)

then, type

python manage.py shell

now you'll be inside python interpreter now import django using:

import django

them import User using this:

from django.contrib.auth.models import User

now take whatever actions you want.

Hope this helps

mcdz
  • 3
  • 3