2016-04-24 30 views
0

我想弄清楚框架是如何在幕後運行的,並且已經成功地使用了pdb.set_trace(),並且我已經能夠將語句記錄到控制檯,但是當我嘗試導入日誌記錄和日誌變量,像這樣,只是作爲一個例子(故意選擇了一個字符串變量):如何在django源代碼中記錄變量

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

什麼是'row_ender'? – AKS

+0

有效的問題;修正我的示例以顯示此功能。 – fstopzero

+0

我不明白。爲什麼在'self._html_output('函數調用作爲參數)中記錄'logging' – AKS

回答

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) 
0

logging不是sys模塊中,所以你不應該在那裏匯入。它本身就是一個獨立的模塊。你所需要的只是import logging