将LOGGING字段添加到settings.py会导致"未找到:/"每个页面加载

时间:2015-09-21 20:05:24

标签: python django logging

c:\Dev>django-admin startproject mysite

将以下内容(从https://docs.djangoproject.com/en/1.8/topics/logging/复制)添加到c:\ Dev \ mysite \ mysite \ settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'c:/Dev/mysite/debug.log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

迁移,运行开发服务器,然后在浏览器中访问http://127.0.0.1:8000/

c:\Dev>cd mysite
c:\Dev\mysite>python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK

c:\Dev\mysite>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
September 21, 2015 - 15:52:25
Django version 1.8.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[21/Sep/2015 15:52:27] "GET / HTTP/1.1" 200 1767

为什么每个页面加载Not Found: /

  • c:\Dev\mysite\debug.log的日志文件也包含Not Found: /
  • 从settings.py中删除LOGGING行可解决此问题。
  • 标准"它有效!"页面本身加载正确的GET 200。

1 个答案:

答案 0 :(得分:0)

groupType中的代码正在记录"Not Found: /"(在django.core.handlers.base.py方法中)。您不会看到您是否未配置日志记录,但如果您执行配置日志记录并且具有“/”URL的处理程序,则会看到它你的urlconfs。

您在日志输出中看到的200是因为Django使用默认响应处理了404个案例。