2010-06-11 39 views
1

我剛開始將django_digest集成到我的應用程序中。作爲開始,我已將@httpdigest裝飾器添加到我的某個視圖中。 如果我嘗試連接它,我得到一個在django_digest/backend/db.py中引發的KeyError異常。根據我配置哪個數據庫,我在不同的位置得到不同的KeyError。我正在使用Django 1.2.1,使用MySql(也使用sqlite測試過)。 我正在使用所有設置選項的默認值。在django-digest的數據庫後端獲取KeyError

據我所見,我遵循了所有的指示,但我整天都在苦苦掙扎。我正在使用django-digest和python-digest的版本庫。任何指導將不勝感激。

回溯了SQLite和MySQL的下面:使用SQLite

Traceback (most recent call last): 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 674, in __call__ 
    return self.application(environ, start_response) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 248, in __call__ 
    signals.request_finished.send(sender=self.__class__) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/dispatch/dispatcher.py", line 162, in send 
    response = receiver(signal=self, sender=sender, **named) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django_digest-1.8-py2.5.egg/django_digest/backend/db.py", line 16, in close_connection 
    _connection.close() 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/db/backends/sqlite3/base.py", line 186, in close 
    if self.settings_dict['NAME'] != ":memory:": 
KeyError: 'NAME' 

與MySQL:

Traceback (most recent call last): 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 674, in __call__ 
    return self.application(environ, start_response) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 241, in __call__ 
    response = self.get_response(request) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/handlers/base.py", line 142, in get_response 
    return self.handle_uncaught_exception(request, resolver, exc_info) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/handlers/base.py", line 166, in handle_uncaught_exception 
    return debug.technical_500_response(request, *exc_info) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/core/handlers/base.py", line 80, in get_response 
    response = middleware_method(request) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django_digest-1.8-py2.5.egg/django_digest/middleware.py", line 13, in process_request 
    if (not self._authenticator.authenticate(request) and 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django_digest-1.8-py2.5.egg/django_digest/__init__.py", line 86, in authenticate 
    partial_digest = self._account_storage.get_partial_digest(digest_response.username) 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django_digest-1.8-py2.5.egg/django_digest/backend/db.py", line 97, in get_partial_digest 
    cursor = get_connection().cursor() 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/db/backends/__init__.py", line 75, in cursor 
    cursor = self._cursor() 
    File "/home/robm/projects/gcs/server/gcs2.5/lib/python2.5/site-packages/django/db/backends/mysql/base.py", line 281, in _cursor 
    if settings_dict['USER']: 
KeyError: 'USER' 

更新 - 似乎這是特定於Django的1.2,多數據庫後端。修改數據庫中的包裝django_digest的後端/ db.py來自:

_connection = backend.DatabaseWrapper({           
     'DATABASE_HOST': settings.DATABASE_HOST,         
     'DATABASE_NAME': settings.DATABASE_NAME,         
     'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,       
     'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,       
     'DATABASE_PORT': settings.DATABASE_PORT,         
     'DATABASE_USER': settings.DATABASE_USER,         
     'TIME_ZONE': settings.TIME_ZONE,           
     })                  

喜歡的東西:

_connection = backend.DatabaseWrapper({ 
       'HOST': settings.DATABASE_HOST, 
       'NAME': settings.DATABASE_NAME, 
       'OPTIONS': settings.DATABASE_OPTIONS, 
       'PASSWORD': settings.DATABASE_PASSWORD, 
       'PORT': settings.DATABASE_PORT, 
       'USER': settings.DATABASE_USER, 
       'TIME_ZONE': settings.TIME_ZONE, 
       }) 

應該修復它。

現在我已經轉向使用HTTPBasicAuth而不是https,但希望下週我會有機會做一個補丁。

回答

0

只是使這個問題表明的回答我將我在評論中添加上述作爲一個答案的解決方案(這是應該做的正確的事嗎?):

似乎這是特定於Django的1.2,多數據庫後端。修改數據庫中的包裝django_digest的後端/ db.py來自:

_connection = backend.DatabaseWrapper({           
    'DATABASE_HOST': settings.DATABASE_HOST,         
    'DATABASE_NAME': settings.DATABASE_NAME,         
    'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,       
    'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,       
    'DATABASE_PORT': settings.DATABASE_PORT,         
    'DATABASE_USER': settings.DATABASE_USER,         
    'TIME_ZONE': settings.TIME_ZONE,           
    })   

喜歡的東西:

_connection = backend.DatabaseWrapper({ 
      'HOST': settings.DATABASE_HOST, 
      'NAME': settings.DATABASE_NAME, 
      'OPTIONS': settings.DATABASE_OPTIONS, 
      'PASSWORD': settings.DATABASE_PASSWORD, 
      'PORT': settings.DATABASE_PORT, 
      'USER': settings.DATABASE_USER, 
      'TIME_ZONE': settings.TIME_ZONE, 
      }) 

應該修復它。