Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
392f6484
Kaydet (Commit)
392f6484
authored
Eyl 11, 2015
tarafından
Nick Johnson
Kaydeden (comit)
Tim Graham
Eyl 21, 2015
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #25373 -- Added warning logging for exceptions during {% include %} tag rendering.
üst
85c52743
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
8 deletions
+75
-8
loader_tags.py
django/template/loader_tags.py
+10
-0
builtins.txt
docs/ref/templates/builtins.txt
+8
-3
1.9.txt
docs/releases/1.9.txt
+9
-2
test_logging.py
tests/template_tests/test_logging.py
+48
-3
No files found.
django/template/loader_tags.py
Dosyayı görüntüle @
392f6484
import
logging
from
collections
import
defaultdict
from
collections
import
defaultdict
from
django.utils
import
six
from
django.utils
import
six
...
@@ -12,6 +13,8 @@ register = Library()
...
@@ -12,6 +13,8 @@ register = Library()
BLOCK_CONTEXT_KEY
=
'block_context'
BLOCK_CONTEXT_KEY
=
'block_context'
logger
=
logging
.
getLogger
(
'django.template'
)
class
ExtendsError
(
Exception
):
class
ExtendsError
(
Exception
):
pass
pass
...
@@ -207,6 +210,13 @@ class IncludeNode(Node):
...
@@ -207,6 +210,13 @@ class IncludeNode(Node):
except
Exception
:
except
Exception
:
if
context
.
template
.
engine
.
debug
:
if
context
.
template
.
engine
.
debug
:
raise
raise
template_name
=
getattr
(
context
,
'template_name'
,
None
)
or
'unknown'
logger
.
warn
(
"Exception raised while rendering {
%%
include
%%
} for "
"template '
%
s'. Empty string rendered instead."
,
template_name
,
exc_info
=
True
,
)
return
''
return
''
...
...
docs/ref/templates/builtins.txt
Dosyayı görüntüle @
392f6484
...
@@ -673,9 +673,14 @@ if it's missing or has syntax errors), the behavior varies depending on the
...
@@ -673,9 +673,14 @@ if it's missing or has syntax errors), the behavior varies depending on the
:class:`template engine's <django.template.Engine>` ``debug`` option (if not
:class:`template engine's <django.template.Engine>` ``debug`` option (if not
set, this option defaults to the value of :setting:`DEBUG`). When debug mode is
set, this option defaults to the value of :setting:`DEBUG`). When debug mode is
turned on, an exception like :exc:`~django.template.TemplateDoesNotExist` or
turned on, an exception like :exc:`~django.template.TemplateDoesNotExist` or
:exc:`~django.template.TemplateSyntaxError` will be raised; otherwise
:exc:`~django.template.TemplateSyntaxError` will be raised. When debug mode
``{% include %}`` silences any exception that happens while rendering the
is turned off, ``{% include %}`` logs a warning to the ``django.template``
included template and returns an empty string.
logger with the exception that happens while rendering the included template
and returns an empty string.
.. versionchanged:: 1.9
Template logging now includes the warning logging mentioned above.
.. note::
.. note::
The :ttag:`include` tag should be considered as an implementation of
The :ttag:`include` tag should be considered as an implementation of
...
...
docs/releases/1.9.txt
Dosyayı görüntüle @
392f6484
...
@@ -596,8 +596,15 @@ Templates
...
@@ -596,8 +596,15 @@ Templates
* Added a :meth:`Context.setdefault() <django.template.Context.setdefault>`
* Added a :meth:`Context.setdefault() <django.template.Context.setdefault>`
method.
method.
* A warning will now be logged for missing context variables. These messages
* The :ref:`django.template <django-template-logger>` logger was added and
will be logged to the :ref:`django.template <django-template-logger>` logger.
includes the following messages:
* A ``DEBUG`` level message for missing context variables.
* A ``WARNING`` level message for uncaught exceptions raised
during the rendering of an ``{% include %}`` when debug mode is off
(helpful since ``{% include %}`` silences the exception and returns an
empty string).
* The :ttag:`firstof` template tag supports storing the output in a variable
* The :ttag:`firstof` template tag supports storing the output in a variable
using 'as'.
using 'as'.
...
...
tests/template_tests/test_logging.py
Dosyayı görüntüle @
392f6484
...
@@ -2,7 +2,7 @@ from __future__ import unicode_literals
...
@@ -2,7 +2,7 @@ from __future__ import unicode_literals
import
logging
import
logging
from
django.template
import
Engine
,
Variable
,
VariableDoesNotExist
from
django.template
import
Context
,
Engine
,
Variable
,
VariableDoesNotExist
from
django.test
import
SimpleTestCase
from
django.test
import
SimpleTestCase
...
@@ -15,18 +15,22 @@ class TestHandler(logging.Handler):
...
@@ -15,18 +15,22 @@ class TestHandler(logging.Handler):
self
.
log_record
=
record
self
.
log_record
=
record
class
VariableResolveLoggingTests
(
SimpleTestCase
):
class
BaseTemplateLoggingTestCase
(
SimpleTestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
test_handler
=
TestHandler
()
self
.
test_handler
=
TestHandler
()
self
.
logger
=
logging
.
getLogger
(
'django.template'
)
self
.
logger
=
logging
.
getLogger
(
'django.template'
)
self
.
original_level
=
self
.
logger
.
level
self
.
original_level
=
self
.
logger
.
level
self
.
logger
.
addHandler
(
self
.
test_handler
)
self
.
logger
.
addHandler
(
self
.
test_handler
)
self
.
logger
.
setLevel
(
logging
.
DEBUG
)
self
.
logger
.
setLevel
(
self
.
loglevel
)
def
tearDown
(
self
):
def
tearDown
(
self
):
self
.
logger
.
removeHandler
(
self
.
test_handler
)
self
.
logger
.
removeHandler
(
self
.
test_handler
)
self
.
logger
.
level
=
self
.
original_level
self
.
logger
.
level
=
self
.
original_level
class
VariableResolveLoggingTests
(
BaseTemplateLoggingTestCase
):
loglevel
=
logging
.
DEBUG
def
test_log_on_variable_does_not_exist_silent
(
self
):
def
test_log_on_variable_does_not_exist_silent
(
self
):
class
TestObject
(
object
):
class
TestObject
(
object
):
class
SilentDoesNotExist
(
Exception
):
class
SilentDoesNotExist
(
Exception
):
...
@@ -78,3 +82,44 @@ class VariableResolveLoggingTests(SimpleTestCase):
...
@@ -78,3 +82,44 @@ class VariableResolveLoggingTests(SimpleTestCase):
def
test_no_log_when_variable_exists
(
self
):
def
test_no_log_when_variable_exists
(
self
):
Variable
(
'article.section'
)
.
resolve
({
'article'
:
{
'section'
:
'News'
}})
Variable
(
'article.section'
)
.
resolve
({
'article'
:
{
'section'
:
'News'
}})
self
.
assertIsNone
(
self
.
test_handler
.
log_record
)
self
.
assertIsNone
(
self
.
test_handler
.
log_record
)
class
IncludeNodeLoggingTests
(
BaseTemplateLoggingTestCase
):
loglevel
=
logging
.
WARN
@classmethod
def
setUpClass
(
cls
):
super
(
IncludeNodeLoggingTests
,
cls
)
.
setUpClass
()
cls
.
engine
=
Engine
(
loaders
=
[
(
'django.template.loaders.locmem.Loader'
,
{
'child'
:
'{{ raises_exception }}'
,
}),
],
debug
=
False
)
def
error_method
():
raise
IndexError
(
"some generic exception"
)
cls
.
ctx
=
Context
({
'raises_exception'
:
error_method
})
def
test_logs_exceptions_during_rendering_with_debug_disabled
(
self
):
template
=
self
.
engine
.
from_string
(
'{
%
include "child"
%
}'
)
template
.
name
=
'template_name'
self
.
assertEqual
(
template
.
render
(
self
.
ctx
),
''
)
self
.
assertEqual
(
self
.
test_handler
.
log_record
.
getMessage
(),
"Exception raised while rendering {
%
include
%
} for template "
"'template_name'. Empty string rendered instead."
)
self
.
assertIsNotNone
(
self
.
test_handler
.
log_record
.
exc_info
)
self
.
assertEqual
(
self
.
test_handler
.
log_record
.
levelno
,
logging
.
WARN
)
def
test_logs_exceptions_during_rendering_with_no_template_name
(
self
):
template
=
self
.
engine
.
from_string
(
'{
%
include "child"
%
}'
)
self
.
assertEqual
(
template
.
render
(
self
.
ctx
),
''
)
self
.
assertEqual
(
self
.
test_handler
.
log_record
.
getMessage
(),
"Exception raised while rendering {
%
include
%
} for template "
"'unknown'. Empty string rendered instead."
)
self
.
assertIsNotNone
(
self
.
test_handler
.
log_record
.
exc_info
)
self
.
assertEqual
(
self
.
test_handler
.
log_record
.
levelno
,
logging
.
WARN
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment