0

我试图诊断我的自定义中间件中的错误以某种方式降低了我的会话中间件。自定义中间件是一个简单的应用程序,可以删除某些文件夹中的旧文件。

class DeleteMediaMiddleware(object):
    time_limit = 100.

    def check_folder(self,folder):
    '''Deletes the files older than "time_limit" '''
        os.chdir(folder)
        files_in_folder = os.listdir(os.curdir)
        for my_file in files_in_folder:
            creation_time = os.path.getmtime(my_file)
            file_lifetime = time.time() - creation_time
            if file_lifetime > self.time_limit:
                os.remove(my_file)

    def process_request(self, request):
    '''Standard middleware method which runs on every request'''
        self.check_folder(config.input_folder)
        self.check_folder(config.output_folder)
        self.check_folder(config.plot_folder)
        self.check_folder(config.report_folder)
    return None

它位于 Django 项目文件夹中。(级别是 Django 默认创建的方式。在我的例子中,项目名为 expofit,文件夹结构为 expofit/expofit)

project_folder_lvl1/project_folder_lvl2/mymiddleware.py

我将中间件添加到 Djagno 设置中:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'server.mymiddleware.DeleteMediaMiddleware',
    )

但是,我最终遇到了错误。

Exception Value:    no such table: django_session

这是通过检查views.py中的会话值:

....    
if request.session.has_key('user_id'):
....

一旦我在 Django 设置中禁用了我的中间件,一切正常。

更新:

我成功打开了数据库,这是一个位于 project_folder_lvl1 中的 sqlite 文件,并找到了无法访问的表。

这是完整的 Django 回溯。

Environment:

Request Method: GET
Request URL: http://localhost:8000/expofit/

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'expofit_django_app',
'south',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'server.mymiddleware.DeleteMediaMiddleware')


Traceback:
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
77.         return view_func(*args, **kwargs)
File "/home/alan/Desktop/expofit/expofit_hg/py/server/expofit_django_app/views.py" in check
59.         return view(request, *args, **kwargs)
File "/home/alan/Desktop/expofit/expofit_hg/py/server/expofit_django_app/views.py" in start
23.     if request.session.has_key('user_id'):
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in has_key
103.         return key in self._session
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in _get_session
165.                 self._session_cache = self.load()
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in load
19.                 expire_date__gt=timezone.now()
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/manager.py" in get
131.         return self.get_query_set().get(*args, **kwargs)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
361.         num = len(clone)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
85.                 self._result_cache = list(self.iterator())
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
291.         for row in compiler.results_iter():
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
763.         for rows in self.execute_sql(MULTI):
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
818.         cursor.execute(sql, params)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
40.             return self.cursor.execute(sql, params)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
337.             return Database.Cursor.execute(self, query, params)

Exception Type: DatabaseError at /expofit/
Exception Value: no such table: django_session

寻找有关如何调试此问题的想法?

4

1 回答 1

0

通过添加到 sqlite 数据库的绝对路径来解决问题。

产生错误的settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'expofit_database',  
        ...
        ...      

新设置.py:

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join( PROJECT_PATH, 'expofit_database'),  
        ...
        ...      

据我所知,问题在于调用

os.chdir(folder)

在弄乱运行时路径的 check_folder 函数中。尽管设置绝对路径解决了访问数据库的问题,但我删除了 os.chdir(folder) 以防止将来出现潜在错误。

def check_folder(folder):
    time_limit = 100.
    files_in_folder = os.listdir(folder)
    for file_name in files_in_folder:
        file_path = os.path.join(folder,file_name)
        creation_time = os.path.getmtime(file_path)
        file_lifetime = time.time() - creation_time
        if file_lifetime > time_limit:
            os.remove(file_path)
于 2012-12-21T15:24:07.347 回答