• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I've tried everything that I could find to fix this issue, and I'm starting to tear my hair out a little. I'm getting this error:

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.

My scripts run fine when I do:

python3 ./manage.py runserver

However, whenever I try to run tests, I get the above error... I use a VirtualEnv, that inherits nothing globally, everything is installed (with the correct version), and within my manage.py I have set:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")

I'm using PyCharm Proffesional to develop, and I've tried running the tests in both the IDE, and in the shell. Within the shell I'm using :

python3 manage.py test     

The shell is finding no tests. The test is basic, and I'm not really all that bothered about the content of it currently as it's the environment I'm struggling with. UPDATE: I have solved the issue with the shell. Tests must be defined by:

def test_<name>():

However this hasn't solved my issue with PyCharm. I have also called:

settings.configure()

Which told me that it was already configured.
Please note that I am not using any database with Django, and I have commented the appropriate things out of the settings.

The full error is:

  Traceback (most recent call last):
  File "/root/kiloenv/lib/python3.4/site-packages/django/conf/__init__.py", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "/usr/lib/python3.4/os.py", line 631, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/<username>/Documents/<dirname>/<appname>/tests.py", line 1, in <module>
        from django.test.utils import setup_test_environment
      File "/root/virtualenv/lib/python3.4/site-packages/django/test/__init__.py", line 5, in <module>
        from django.test.client import Client, RequestFactory
      File "/root/virtualenv/lib/python3.4/site-packages/django/test/client.py", line 11, in <module>
        from django.contrib.auth import authenticate, login, logout, get_user_model
      File "/root/virtualenv/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 6, in <module>
        from django.middleware.csrf import rotate_token
      File "/root/virtualenv/lib/python3.4/site-packages/django/middleware/csrf.py", line 14, in <module>
        from django.utils.cache import patch_vary_headers
      File "/root/virtualenv/lib/python3.4/site-packages/django/utils/cache.py", line 26, in <module>
        from django.core.cache import get_cache
      File "/root/virtualenv/lib/python3.4/site-packages/django/core/cache/__init__.py", line 69, in <module>
        if DEFAULT_CACHE_ALIAS not in settings.CACHES:
      File "/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 54, in __getattr__
        self._setup(name)
      File "/root/virtualenv/lib/python3.4/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.

I have ran PyCharm under sudo to ensure that the issue wasn't permissions as I'm storing the env under root.

EDIT: I've just discovered that my tests which do not use Django are running fine, but Pycharm is still throwing several failures. These failures aren't the individual tests, they're just the error which I have mentioned here (there's 341 tests that aren't Django related). I only have one test which uses Django, which will not get past initializing and throwing the mentioned error.

Hope I've been explanitory

      • 1
    • Running things as sudo is the answer - when the question is "how can I mess up the permissions on my home folder".

In your python script, you are trying to access Django models before setting the environment try it in this order :

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.`settings`")
from <appname>.models import Class1, Class2,...
  • 10
Reply Report

If you are using PyCharm Pro, you can either test your app by action 'Run Django Console...'. After you click 'test', it will prompt you for the app you want to test.

enter image description here

or

Create a Django test in Run/Debug configuration.

enter image description here

  • 9
Reply Report

since, I don't want to add this in every file, I created a file called usercustomize.py and added this content:

import os
os.environ['DJANGO_SETTINGS_MODULE']='foosite.settings'

The file path in my case:

src/foosite/usercustomize.py

I installed foosite with "pip install -e". I am unsure if this works for normal installs.

  • 2
Reply Report
      • 2
    • whaaaaaaaaaaattttt ??? make and example please, so, you put that under a file, and just import it everytime you need it ? where did you putted that file ? what dir ? how do you installed ?
      • 2
    • I updated my answer. We use usercustomize.py now, since some linux distros have their own sitecustomize.py. You can put this file anywhere on your sys.path. You asked how I installed it: I installed it editable. This means inside ~/src/.

I got this error when I imported TestCase from unittest2. I fixed it by importing TestCase by:

from django.test import TestCase

My suspicions were raised when the test was labelled "Unittest" rather than "Test". I had to run the whole suite before Pycharm realised I'd made this change...

  • 0
Reply Report

Trending Tags