Why does Django say that I have not set DATABASE_ENGINE yet?

I have a Django project and I'm a little new to it. I have the following PyUnit test trying to save an object in a PostgreSQL database:

import unittest

from foo.models import ObjectType

class DbTest(unittest.TestCase):
    def testDBConnection(self):
        object_type = ObjectType()

        object_type.name = 'Test Object'
        object_type.description = 'For testing purposes'

        object_type.save()

And my database settings are as follows:

DATABASE_ENGINE = 'postgresql_psycopg2'           
DATABASE_NAME = 'FOO'             
DATABASE_USER = 'username'            
DATABASE_PASSWORD = 'password'         
DATABASE_HOST = 'localhost'            
DATABASE_PORT = '5432'    

I ran python manage.py syncdb for my project that worked successfully, so my database needs to be configured correctly. However, when trying to run the unit test above:

I get the following error:

File "C: \ Python26 \ Lib \ site-packages \ Django \ DB \ engines \ dummy \ base.py", line 15, in the complaint Raise Incorrectly configured: "You have not set DATABASE_ENGINE yet." Incorrect Configured: You have not set the DATABASE_ENGINE installation yet.

- - ? , , ? !

+3
3

, , , .. python testfile.py. , Django . , (Django, settings.py ), , , . , , Django.

Django ( ). , .. , .

+3

Django , . Django, , :

from django.conf import settings
settings.configure(
        DATABASE_ENGINE='postgresql_psycopg2',
        DATABASE_NAME='FOO',
        DATABASE_USER='username',
        DATABASE_PASSWORD='password',
        DATABASE_HOST='localhost',
        DATABASE_PORT='5432',
        INSTALLED_APPS=('foo',)

)

, , , , :

import django.core.management
from foo import settings
django.core.management.setup_environ(settings)
+2

DjANGO_SETTINGS_MODULE = myapp.settings .

Linux/ bash, : export DJANGO_SETTINGS_MODULE = myapp.settings

+2

Source: https://habr.com/ru/post/1725521/


All Articles