我正在试图弄清楚框架是如何在幕后运行的,并且已经成功使用pdb.set_trace(),并且我已经能够将语句记录到控制台,但是当我尝试导入日志记录时日志变量,就像这样,仅作为示例(故意选择字符串变量):
def as_ul(self):
"Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
import pdb; pdb.set_trace()
return self._html_output(
normal_row='<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>',
error_row='<li>%s</li>',
row_ender='</li>',
help_text_html=' <span class="helptext">%s</span>',
logging.warning('%s', row_ender)
errors_on_separate_row=False)
我收到错误(下面的完整追溯)
在页面顶部,我添加了from sys import logging
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 195, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 39, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 13, in <module>
from django.core.servers.basehttp import get_internal_wsgi_application, run
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 17, in <module>
from django.core.handlers.wsgi import ISO_8859_1, UTF_8
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 10, in <module>
from django import http
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/http/__init__.py", line 5, in <module>
from django.http.response import (
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/http/response.py", line 13, in <module>
from django.core.serializers.json import DjangoJSONEncoder
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 23, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/core/serializers/base.py", line 4, in <module>
from django.db import models
File "/Users/user/Documents/repos/djangoFormDissection/env/lib/python2.7/site-packages/django/db/models/__init__.py", line 4, in <module>
from django.db.models import signals # NOQA
ImportError: cannot import name signals
答案 0 :(得分:0)
您使用logging.warning('%s', row_ender)
作为self._html_output(
函数的参数:
def as_ul(self):
"Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
import pdb; pdb.set_trace()
row_ender = '</li>'
logging.warning('%s', row_ender)
return self._html_output(
normal_row='<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>',
error_row='<li>%s</li>',
row_ender=row_ender, # use the variable here
help_text_html=' <span class="helptext">%s</span>',
errors_on_separate_row=False)
答案 1 :(得分:0)
logging
不在sys
模块中,因此您不应该从那里导入它。它本身就是一个独立的模块。您所需要的只是import logging
。