Repository: jazzband/django-debug-toolbar

Issue:
AttributeError: 'SafeString' object has no attribute 'pygmentized'
Django Version: 4.1
Python Version: 3.10
Django Debug Toolbar Version: 3.5.0

Steps to Reproduce:
1. Open the toolbar
2. Click Templates
3. Click on a Specific Template

<img width="1714" alt="Screen Shot 2022-08-16 at 1 41 17 PM" src="https://user-images.githubusercontent.com/22578954/184944289-b648cb1c-6db7-4bf3-81e4-ce2682b13cfd.png">


```
ERROR 2022-08-16 12:29:00,535 log 82 275499009792 Internal Server Error: /__debug__/template_source/
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 472, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
  File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 435, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 408, in wait_for
    return await fut
  File "/usr/local/lib/python3.10/site-packages/asgiref/current_thread_executor.py", line 22, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 476, in thread_handler
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/decorators.py", line 15, in inner
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/templates/views.py", line 56, in template_source
    source.pygmentized = True
AttributeError: 'SafeString' object has no attribute 'pygmentized'
```


Discussion:
Ah yes, that's caused by https://code.djangoproject.com/ticket/33465

We should use a standalone variable for the `pygmentized` flag instead of attaching it to `source`.

Do you want to attempt fixing this?

Resolving patch:
diff --git a/debug_toolbar/panels/templates/views.py b/debug_toolbar/panels/templates/views.py
--- a/debug_toolbar/panels/templates/views.py
+++ b/debug_toolbar/panels/templates/views.py
@@ -3,7 +3,7 @@
 from django.template import Origin, TemplateDoesNotExist
 from django.template.engine import Engine
 from django.template.loader import render_to_string
-from django.utils.safestring import mark_safe
+from django.utils.html import format_html, mark_safe
 
 from debug_toolbar.decorators import require_show_toolbar
 
@@ -50,12 +50,11 @@ def template_source(request):
         from pygments import highlight
         from pygments.formatters import HtmlFormatter
         from pygments.lexers import HtmlDjangoLexer
-
+    except ModuleNotFoundError:
+        source = format_html("<code>{}</code>", source)
+    else:
         source = highlight(source, HtmlDjangoLexer(), HtmlFormatter())
         source = mark_safe(source)
-        source.pygmentized = True
-    except ImportError:
-        pass
 
     content = render_to_string(
         "debug_toolbar/panels/template_source.html",


Test patch: