pythonxmlexist-db

Configuring eulexistdb with python bringing errors in django setting module


I have following code written in python in order to communicate with ExistDB using eulexistdb module.

from eulexistdb import db    
class TryExist:    
    def __init__(self):
        self.db = db.ExistDB(server_url="http://localhost:8899/exist")    
    def get_data(self, query):
        result = list()
        qresult = self.db.executeQuery(query)
        hits = self.db.getHits(qresult)
        for i in range(hits):
            result.append(str(self.db.retrieve(qresult, i)))
        return result

query = '''
let $x:= doc("/db/sample/books.xml")
return $x/bookstore/book/author/text()
'''
a = TryExist()
response = a.get_data(query)
print response

I am amazed that this code runs fine in Aptana Studio 3 giving me the output I want, but when running from other IDE or using command "python.exe myfile.py" brings following error:

django.core.exceptions.ImproperlyConfigured: Requested setting EXISTDB_TIMEOUT, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I used my own localsetting.py to solve the problem using following code:

import os
# must be set before importing anything from django
os.environ['DJANGO_SETTINGS_MODULE'] = 'localsettings'
... writing link for existdb here...

Then I get error as:

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

How do I configure the setting in Django to suit for ExistDB? Help me here please..


Solution

  • Never Mind. I found the answer with little research from this site. What I did was created a localsetting.py file with following configurations.

    EXISTDB_SERVER_USER = 'user'
    EXISTDB_SERVER_PASSWORD = 'admin'
    EXISTDB_SERVER_URL = "http://localhost:8899/exist"
    EXISTDB_ROOT_COLLECTION = "/db"
    

    and in my main file myfile.py I used :

    from localsettings import EXISTDB_SERVER_URL
    import os
    os.environ['DJANGO_SETTINGS_MODULE'] = 'localsettings.py'
    

    and In the class TryExist I changed in __ init __() as:

    def __init__(self):
            self.db = db.ExistDB(server_url=EXISTDB_SERVER_URL)
    

    PS: Using only os.environ['DJANGO_SETTINGS_MODULE'] = 'localsettings' brings the django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty..