Problem in importing User models

Asked

Viewed 255 times

2

I am doing some tests in a Django project. I created a very simple module just for testing. Below:

from django.contrib.auth.models import User

def teste():
    for i in User.objects.all():
        print i.username

if __name__ == "__main__":
    teste()

When executing the code, this error is returned:

Traceback (Most recent call last): File "test.py", line 6, in from Django.contrib.auth.models import User File "/home/carlos/myworkspace/Helpdesk/lib/python2.7/site-Packages/Django /contrib/auth/init.py", line 6, in from Django.middleware.csrf import rotate_token File "/home/carlos/myworkspace/Helpdesk/lib/python2.7/site-Packages/Django /middleware/csrf.py", line 14, in from Django.utils.cache import patch_vary_headers File "/home/carlos/myworkspace/Helpdesk/lib/python2.7/site-Packages/Django/utils/cache.py", line 26, in from Django.core.cache import get_cache File "/home/carlos/myworkspace/Helpdesk/lib/python2.7/site-Packages/Django/core/cache/init.py", line 69, in if DEFAULT_CACHE_ALIAS not in Settings.CACHES: File "/home/carlos/myworkspace/Helpdesk/lib/python2.7/site-Packages/Django/conf/init.py", line 54, in getattr self. _setup(name) File "/home/carlos/myworkspace/Helpdesk/lib/python2.7/site-Packages/Django/conf/init.py", line 47, in _setup % (desc, ENVIRONMENT_VARIABLE)) Django.core.exceptions.Improperlyconfigured: Requested Setting CACHES, but Settings are not configured. You must either define the Environment variable DJANGO_SETTINGS_MODULE or call Settings.configure() before accessing Settings.

NOTE: Inside the python manage.py shell with the %run test.py command, the code works.

1 answer

1

This is not the "common" way to run a project with Django, but if you just want to solve the problem in question, the solution is to set the variable DJANGO_SETTINGS_MODULE.

When you turn the command by python manage.py shell, this variable is set automatically, which causes in this case the error does not occur.

To set the variable manually, you can put the following code at the beginning of your script:

import os
# estou considerando que o nome do projeto (e não o app!) seja "helpdesk"
os.environ['DJANGO_SETTINGS_MODULE'] = 'helpdesk.settings' 

Another option is to set the variable in your shell instead of the code. So:

export DJANGO_SETTINGS_MODULE=helpdesk.settings

In any case, I recommend that you do not do so. It is simpler and more reliable to use the manage.py, or make a code "traditional", using templates, views and templates.

  • Now this returning : Import: Could not import Settings 'help.Settings' (Is it on sys.path? Is there an import error in the Settings file?): No module named help.Settings

  • my path /home/carlos/myworkspace/Helpdesk/help/ /home/carlos/myworkspace/Helpdesk /home/carlos/myworkspace/Helpdesk/lib/python27.zip /home/carlos/myworkspace/Helpdesk/lib64/python2.7 /home/carlos/myworkspace/Helpdesk/lib64/python2.7 /home/carlos/myworkspace/Helpdesk/lib64/python2.7/lib-old /home/carlos/myworkspace/Helpdesk/lib64/python2.7/lib-dynload /usr/lib64/python2.7 /usr/lib/python2.7 ...

  • meu wsgi : 
import os
import sys

sys.path.append('/home/carlos/myworkspace/helpdesk/help')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "help.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

  • "help" is the name of the app or project? Have you tried with "Helpdesk.Settings" instead of "help.Settings"? Another option is to set DJANGO_SETTINGS_MODULE as an environment variable, like this: export DJANGO_SETTINGS_MODULE="help.settings"

  • "help" is the name of the app and "Helpdesk" is from the project. The Settings file is in the "help" folder. $DJANGO_SETTINGS_MODULE and $PYTHONPATH are right. I am already two days researching and testing and nothing. I will continue and anything put. Thank you Michael.

  • 1

    Ok. Just make sure to use "Helpdesk.Settings" then. The correct thing is to always use the name of the site/project, not the app.

  • If the app is "help", the file settings.py shouldn’t be in the folder help but in the briefcase helpdesk (the project folder, along with the urls.py master and the wsgi.py) (Settings is from the entire site, not from the app and so on helpdesk.settings functionary)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.