在django-digest的DB后端获取KeyError

时间:2010-06-11 22:27:26

标签: python django

我刚刚开始将django_digest集成到我的应用中。首先,我将@httpdigest装饰器添加到我的一个视图中。 如果我尝试连接到它,我会在django_digest / backend / db.py中抛出一个KeyError异常。根据我配置的db,我在不同的位置获得不同的KeyError。我正在使用Django 1.2.1,使用MySql(也使用sqlite进行测试)。 我正在使用所有设置选项的默认值。

据我所知,我已经遵循了所有指示,但我整天都在苦苦挣扎。我正在使用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,但希望下周我有机会做补丁。

1 个答案:

答案 0 :(得分: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,
            })

应该修理它。