PageRenderTime 58ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/python/lib/Lib/site-packages/django/views/debug.py

http://github.com/JetBrains/intellij-community
Python | 876 lines | 837 code | 19 blank | 20 comment | 21 complexity | e63c2d6e35578dbf8f62778230244c5e MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. import datetime
  2. import os
  3. import re
  4. import sys
  5. import types
  6. from django.conf import settings
  7. from django.http import HttpResponse, HttpResponseServerError, HttpResponseNotFound
  8. from django.template import (Template, Context, TemplateDoesNotExist,
  9. TemplateSyntaxError)
  10. from django.utils.html import escape
  11. from django.utils.importlib import import_module
  12. from django.utils.encoding import smart_unicode, smart_str
  13. HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD|PROFANITIES_LIST|SIGNATURE')
  14. def linebreak_iter(template_source):
  15. yield 0
  16. p = template_source.find('\n')
  17. while p >= 0:
  18. yield p+1
  19. p = template_source.find('\n', p+1)
  20. yield len(template_source) + 1
  21. def cleanse_setting(key, value):
  22. """Cleanse an individual setting key/value of sensitive content.
  23. If the value is a dictionary, recursively cleanse the keys in
  24. that dictionary.
  25. """
  26. try:
  27. if HIDDEN_SETTINGS.search(key):
  28. cleansed = '********************'
  29. else:
  30. if isinstance(value, dict):
  31. cleansed = dict((k, cleanse_setting(k, v)) for k,v in value.items())
  32. else:
  33. cleansed = value
  34. except TypeError:
  35. # If the key isn't regex-able, just return as-is.
  36. cleansed = value
  37. return cleansed
  38. def get_safe_settings():
  39. "Returns a dictionary of the settings module, with sensitive settings blurred out."
  40. settings_dict = {}
  41. for k in dir(settings):
  42. if k.isupper():
  43. settings_dict[k] = cleanse_setting(k, getattr(settings, k))
  44. return settings_dict
  45. def technical_500_response(request, exc_type, exc_value, tb):
  46. """
  47. Create a technical server error response. The last three arguments are
  48. the values returned from sys.exc_info() and friends.
  49. """
  50. reporter = ExceptionReporter(request, exc_type, exc_value, tb)
  51. html = reporter.get_traceback_html()
  52. return HttpResponseServerError(html, mimetype='text/html')
  53. class ExceptionReporter:
  54. """
  55. A class to organize and coordinate reporting on exceptions.
  56. """
  57. def __init__(self, request, exc_type, exc_value, tb, is_email=False):
  58. self.request = request
  59. self.exc_type = exc_type
  60. self.exc_value = exc_value
  61. self.tb = tb
  62. self.is_email = is_email
  63. self.template_info = None
  64. self.template_does_not_exist = False
  65. self.loader_debug_info = None
  66. # Handle deprecated string exceptions
  67. if isinstance(self.exc_type, basestring):
  68. self.exc_value = Exception('Deprecated String Exception: %r' % self.exc_type)
  69. self.exc_type = type(self.exc_value)
  70. def get_traceback_html(self):
  71. "Return HTML code for traceback."
  72. if issubclass(self.exc_type, TemplateDoesNotExist):
  73. from django.template.loader import template_source_loaders
  74. self.template_does_not_exist = True
  75. self.loader_debug_info = []
  76. for loader in template_source_loaders:
  77. try:
  78. module = import_module(loader.__module__)
  79. source_list_func = module.get_template_sources
  80. # NOTE: This assumes exc_value is the name of the template that
  81. # the loader attempted to load.
  82. template_list = [{'name': t, 'exists': os.path.exists(t)} \
  83. for t in source_list_func(str(self.exc_value))]
  84. except (ImportError, AttributeError):
  85. template_list = []
  86. if hasattr(loader, '__class__'):
  87. loader_name = loader.__module__ + '.' + loader.__class__.__name__
  88. else:
  89. loader_name = loader.__module__ + '.' + loader.__name__
  90. self.loader_debug_info.append({
  91. 'loader': loader_name,
  92. 'templates': template_list,
  93. })
  94. if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source') and
  95. isinstance(self.exc_value, TemplateSyntaxError)):
  96. self.get_template_exception_info()
  97. frames = self.get_traceback_frames()
  98. unicode_hint = ''
  99. if issubclass(self.exc_type, UnicodeError):
  100. start = getattr(self.exc_value, 'start', None)
  101. end = getattr(self.exc_value, 'end', None)
  102. if start is not None and end is not None:
  103. unicode_str = self.exc_value.args[1]
  104. unicode_hint = smart_unicode(unicode_str[max(start-5, 0):min(end+5, len(unicode_str))], 'ascii', errors='replace')
  105. from django import get_version
  106. t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template')
  107. c = Context({
  108. 'is_email': self.is_email,
  109. 'exception_type': self.exc_type.__name__,
  110. 'exception_value': smart_unicode(self.exc_value, errors='replace'),
  111. 'unicode_hint': unicode_hint,
  112. 'frames': frames,
  113. 'lastframe': frames[-1],
  114. 'request': self.request,
  115. 'settings': get_safe_settings(),
  116. 'sys_executable': sys.executable,
  117. 'sys_version_info': '%d.%d.%d' % sys.version_info[0:3],
  118. 'server_time': datetime.datetime.now(),
  119. 'django_version_info': get_version(),
  120. 'sys_path' : sys.path,
  121. 'template_info': self.template_info,
  122. 'template_does_not_exist': self.template_does_not_exist,
  123. 'loader_debug_info': self.loader_debug_info,
  124. })
  125. return t.render(c)
  126. def get_template_exception_info(self):
  127. origin, (start, end) = self.exc_value.source
  128. template_source = origin.reload()
  129. context_lines = 10
  130. line = 0
  131. upto = 0
  132. source_lines = []
  133. before = during = after = ""
  134. for num, next in enumerate(linebreak_iter(template_source)):
  135. if start >= upto and end <= next:
  136. line = num
  137. before = escape(template_source[upto:start])
  138. during = escape(template_source[start:end])
  139. after = escape(template_source[end:next])
  140. source_lines.append( (num, escape(template_source[upto:next])) )
  141. upto = next
  142. total = len(source_lines)
  143. top = max(1, line - context_lines)
  144. bottom = min(total, line + 1 + context_lines)
  145. self.template_info = {
  146. 'message': self.exc_value.args[0],
  147. 'source_lines': source_lines[top:bottom],
  148. 'before': before,
  149. 'during': during,
  150. 'after': after,
  151. 'top': top,
  152. 'bottom': bottom,
  153. 'total': total,
  154. 'line': line,
  155. 'name': origin.name,
  156. }
  157. def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, module_name=None):
  158. """
  159. Returns context_lines before and after lineno from file.
  160. Returns (pre_context_lineno, pre_context, context_line, post_context).
  161. """
  162. source = None
  163. if loader is not None and hasattr(loader, "get_source"):
  164. source = loader.get_source(module_name)
  165. if source is not None:
  166. source = source.splitlines()
  167. if source is None:
  168. try:
  169. f = open(filename)
  170. try:
  171. source = f.readlines()
  172. finally:
  173. f.close()
  174. except (OSError, IOError):
  175. pass
  176. if source is None:
  177. return None, [], None, []
  178. encoding = 'ascii'
  179. for line in source[:2]:
  180. # File coding may be specified. Match pattern from PEP-263
  181. # (http://www.python.org/dev/peps/pep-0263/)
  182. match = re.search(r'coding[:=]\s*([-\w.]+)', line)
  183. if match:
  184. encoding = match.group(1)
  185. break
  186. source = [unicode(sline, encoding, 'replace') for sline in source]
  187. lower_bound = max(0, lineno - context_lines)
  188. upper_bound = lineno + context_lines
  189. pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
  190. context_line = source[lineno].strip('\n')
  191. post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
  192. return lower_bound, pre_context, context_line, post_context
  193. def get_traceback_frames(self):
  194. frames = []
  195. tb = self.tb
  196. while tb is not None:
  197. # support for __traceback_hide__ which is used by a few libraries
  198. # to hide internal frames.
  199. if tb.tb_frame.f_locals.get('__traceback_hide__'):
  200. tb = tb.tb_next
  201. continue
  202. filename = tb.tb_frame.f_code.co_filename
  203. function = tb.tb_frame.f_code.co_name
  204. lineno = tb.tb_lineno - 1
  205. loader = tb.tb_frame.f_globals.get('__loader__')
  206. module_name = tb.tb_frame.f_globals.get('__name__')
  207. pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)
  208. if pre_context_lineno is not None:
  209. frames.append({
  210. 'tb': tb,
  211. 'filename': filename,
  212. 'function': function,
  213. 'lineno': lineno + 1,
  214. 'vars': tb.tb_frame.f_locals.items(),
  215. 'id': id(tb),
  216. 'pre_context': pre_context,
  217. 'context_line': context_line,
  218. 'post_context': post_context,
  219. 'pre_context_lineno': pre_context_lineno + 1,
  220. })
  221. tb = tb.tb_next
  222. if not frames:
  223. frames = [{
  224. 'filename': '&lt;unknown&gt;',
  225. 'function': '?',
  226. 'lineno': '?',
  227. 'context_line': '???',
  228. }]
  229. return frames
  230. def format_exception(self):
  231. """
  232. Return the same data as from traceback.format_exception.
  233. """
  234. import traceback
  235. frames = self.get_traceback_frames()
  236. tb = [ (f['filename'], f['lineno'], f['function'], f['context_line']) for f in frames ]
  237. list = ['Traceback (most recent call last):\n']
  238. list += traceback.format_list(tb)
  239. list += traceback.format_exception_only(self.exc_type, self.exc_value)
  240. return list
  241. def technical_404_response(request, exception):
  242. "Create a technical 404 error response. The exception should be the Http404."
  243. try:
  244. tried = exception.args[0]['tried']
  245. except (IndexError, TypeError, KeyError):
  246. tried = []
  247. else:
  248. if not tried:
  249. # tried exists but is an empty list. The URLconf must've been empty.
  250. return empty_urlconf(request)
  251. urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
  252. if isinstance(urlconf, types.ModuleType):
  253. urlconf = urlconf.__name__
  254. t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 template')
  255. c = Context({
  256. 'urlconf': urlconf,
  257. 'root_urlconf': settings.ROOT_URLCONF,
  258. 'request_path': request.path_info[1:], # Trim leading slash
  259. 'urlpatterns': tried,
  260. 'reason': smart_str(exception, errors='replace'),
  261. 'request': request,
  262. 'settings': get_safe_settings(),
  263. })
  264. return HttpResponseNotFound(t.render(c), mimetype='text/html')
  265. def empty_urlconf(request):
  266. "Create an empty URLconf 404 error response."
  267. t = Template(EMPTY_URLCONF_TEMPLATE, name='Empty URLConf template')
  268. c = Context({
  269. 'project_name': settings.SETTINGS_MODULE.split('.')[0]
  270. })
  271. return HttpResponse(t.render(c), mimetype='text/html')
  272. #
  273. # Templates are embedded in the file so that we know the error handler will
  274. # always work even if the template loader is broken.
  275. #
  276. TECHNICAL_500_TEMPLATE = """
  277. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  278. <html lang="en">
  279. <head>
  280. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  281. <meta name="robots" content="NONE,NOARCHIVE">
  282. <title>{{ exception_type }} at {{ request.path_info|escape }}</title>
  283. <style type="text/css">
  284. html * { padding:0; margin:0; }
  285. body * { padding:10px 20px; }
  286. body * * { padding:0; }
  287. body { font:small sans-serif; }
  288. body>div { border-bottom:1px solid #ddd; }
  289. h1 { font-weight:normal; }
  290. h2 { margin-bottom:.8em; }
  291. h2 span { font-size:80%; color:#666; font-weight:normal; }
  292. h3 { margin:1em 0 .5em 0; }
  293. h4 { margin:0 0 .5em 0; font-weight: normal; }
  294. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  295. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  296. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  297. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  298. table.vars { margin:5px 0 2px 40px; }
  299. table.vars td, table.req td { font-family:monospace; }
  300. table td.code { width:100%; }
  301. table td.code pre { overflow:hidden; }
  302. table.source th { color:#666; }
  303. table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
  304. ul.traceback { list-style-type:none; }
  305. ul.traceback li.frame { margin-bottom:1em; }
  306. div.context { margin: 10px 0; }
  307. div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
  308. div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
  309. div.context ol.context-line li { color:black; background-color:#ccc; }
  310. div.context ol.context-line li span { float: right; }
  311. div.commands { margin-left: 40px; }
  312. div.commands a { color:black; text-decoration:none; }
  313. #summary { background: #ffc; }
  314. #summary h2 { font-weight: normal; color: #666; }
  315. #explanation { background:#eee; }
  316. #template, #template-not-exist { background:#f6f6f6; }
  317. #template-not-exist ul { margin: 0 0 0 20px; }
  318. #unicode-hint { background:#eee; }
  319. #traceback { background:#eee; }
  320. #requestinfo { background:#f6f6f6; padding-left:120px; }
  321. #summary table { border:none; background:transparent; }
  322. #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
  323. #requestinfo h3 { margin-bottom:-1em; }
  324. .error { background: #ffc; }
  325. .specific { color:#cc3300; font-weight:bold; }
  326. h2 span.commands { font-size:.7em;}
  327. span.commands a:link {color:#5E5694;}
  328. pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
  329. </style>
  330. {% if not is_email %}
  331. <script type="text/javascript">
  332. //<!--
  333. function getElementsByClassName(oElm, strTagName, strClassName){
  334. // Written by Jonathan Snook, http://www.snook.ca/jon; Add-ons by Robert Nyman, http://www.robertnyman.com
  335. var arrElements = (strTagName == "*" && document.all)? document.all :
  336. oElm.getElementsByTagName(strTagName);
  337. var arrReturnElements = new Array();
  338. strClassName = strClassName.replace(/\-/g, "\\-");
  339. var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  340. var oElement;
  341. for(var i=0; i<arrElements.length; i++){
  342. oElement = arrElements[i];
  343. if(oRegExp.test(oElement.className)){
  344. arrReturnElements.push(oElement);
  345. }
  346. }
  347. return (arrReturnElements)
  348. }
  349. function hideAll(elems) {
  350. for (var e = 0; e < elems.length; e++) {
  351. elems[e].style.display = 'none';
  352. }
  353. }
  354. window.onload = function() {
  355. hideAll(getElementsByClassName(document, 'table', 'vars'));
  356. hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
  357. hideAll(getElementsByClassName(document, 'ol', 'post-context'));
  358. hideAll(getElementsByClassName(document, 'div', 'pastebin'));
  359. }
  360. function toggle() {
  361. for (var i = 0; i < arguments.length; i++) {
  362. var e = document.getElementById(arguments[i]);
  363. if (e) {
  364. e.style.display = e.style.display == 'none' ? 'block' : 'none';
  365. }
  366. }
  367. return false;
  368. }
  369. function varToggle(link, id) {
  370. toggle('v' + id);
  371. var s = link.getElementsByTagName('span')[0];
  372. var uarr = String.fromCharCode(0x25b6);
  373. var darr = String.fromCharCode(0x25bc);
  374. s.innerHTML = s.innerHTML == uarr ? darr : uarr;
  375. return false;
  376. }
  377. function switchPastebinFriendly(link) {
  378. s1 = "Switch to copy-and-paste view";
  379. s2 = "Switch back to interactive view";
  380. link.innerHTML = link.innerHTML == s1 ? s2 : s1;
  381. toggle('browserTraceback', 'pastebinTraceback');
  382. return false;
  383. }
  384. //-->
  385. </script>
  386. {% endif %}
  387. </head>
  388. <body>
  389. <div id="summary">
  390. <h1>{{ exception_type }}{% if request %} at {{ request.path_info|escape }}{% endif %}</h1>
  391. <pre class="exception_value">{{ exception_value|force_escape }}</pre>
  392. <table class="meta">
  393. <tr>
  394. <th>Request Method:</th>
  395. <td>{{ request.META.REQUEST_METHOD }}</td>
  396. </tr>
  397. <tr>
  398. <th>Request URL:</th>
  399. <td>{{ request.build_absolute_uri|escape }}</td>
  400. </tr>
  401. <tr>
  402. <th>Django Version:</th>
  403. <td>{{ django_version_info }}</td>
  404. </tr>
  405. <tr>
  406. <th>Exception Type:</th>
  407. <td>{{ exception_type }}</td>
  408. </tr>
  409. <tr>
  410. <th>Exception Value:</th>
  411. <td><pre>{{ exception_value|force_escape }}</pre></td>
  412. </tr>
  413. <tr>
  414. <th>Exception Location:</th>
  415. <td>{{ lastframe.filename|escape }} in {{ lastframe.function|escape }}, line {{ lastframe.lineno }}</td>
  416. </tr>
  417. <tr>
  418. <th>Python Executable:</th>
  419. <td>{{ sys_executable|escape }}</td>
  420. </tr>
  421. <tr>
  422. <th>Python Version:</th>
  423. <td>{{ sys_version_info }}</td>
  424. </tr>
  425. <tr>
  426. <th>Python Path:</th>
  427. <td><pre>{{ sys_path|pprint }}</pre></td>
  428. </tr>
  429. <tr>
  430. <th>Server time:</th>
  431. <td>{{server_time|date:"r"}}</td>
  432. </tr>
  433. </table>
  434. </div>
  435. {% if unicode_hint %}
  436. <div id="unicode-hint">
  437. <h2>Unicode error hint</h2>
  438. <p>The string that could not be encoded/decoded was: <strong>{{ unicode_hint|force_escape }}</strong></p>
  439. </div>
  440. {% endif %}
  441. {% if template_does_not_exist %}
  442. <div id="template-not-exist">
  443. <h2>Template-loader postmortem</h2>
  444. {% if loader_debug_info %}
  445. <p>Django tried loading these templates, in this order:</p>
  446. <ul>
  447. {% for loader in loader_debug_info %}
  448. <li>Using loader <code>{{ loader.loader }}</code>:
  449. <ul>{% for t in loader.templates %}<li><code>{{ t.name }}</code> (File {% if t.exists %}exists{% else %}does not exist{% endif %})</li>{% endfor %}</ul>
  450. </li>
  451. {% endfor %}
  452. </ul>
  453. {% else %}
  454. <p>Django couldn't find any templates because your <code>TEMPLATE_LOADERS</code> setting is empty!</p>
  455. {% endif %}
  456. </div>
  457. {% endif %}
  458. {% if template_info %}
  459. <div id="template">
  460. <h2>Template error</h2>
  461. <p>In template <code>{{ template_info.name }}</code>, error at line <strong>{{ template_info.line }}</strong></p>
  462. <h3>{{ template_info.message }}</h3>
  463. <table class="source{% if template_info.top %} cut-top{% endif %}{% ifnotequal template_info.bottom template_info.total %} cut-bottom{% endifnotequal %}">
  464. {% for source_line in template_info.source_lines %}
  465. {% ifequal source_line.0 template_info.line %}
  466. <tr class="error"><th>{{ source_line.0 }}</th>
  467. <td>{{ template_info.before }}<span class="specific">{{ template_info.during }}</span>{{ template_info.after }}</td></tr>
  468. {% else %}
  469. <tr><th>{{ source_line.0 }}</th>
  470. <td>{{ source_line.1 }}</td></tr>
  471. {% endifequal %}
  472. {% endfor %}
  473. </table>
  474. </div>
  475. {% endif %}
  476. <div id="traceback">
  477. <h2>Traceback <span class="commands">{% if not is_email %}<a href="#" onclick="return switchPastebinFriendly(this);">Switch to copy-and-paste view</a></span>{% endif %}</h2>
  478. {% autoescape off %}
  479. <div id="browserTraceback">
  480. <ul class="traceback">
  481. {% for frame in frames %}
  482. <li class="frame">
  483. <code>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code>
  484. {% if frame.context_line %}
  485. <div class="context" id="c{{ frame.id }}">
  486. {% if frame.pre_context and not is_email %}
  487. <ol start="{{ frame.pre_context_lineno }}" class="pre-context" id="pre{{ frame.id }}">{% for line in frame.pre_context %}<li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"><pre>{{ line|escape }}</pre></li>{% endfor %}</ol>
  488. {% endif %}
  489. <ol start="{{ frame.lineno }}" class="context-line"><li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"><pre>{{ frame.context_line|escape }}</pre>{% if not is_email %} <span>...</span>{% endif %}</li></ol>
  490. {% if frame.post_context and not is_email %}
  491. <ol start='{{ frame.lineno|add:"1" }}' class="post-context" id="post{{ frame.id }}">{% for line in frame.post_context %}<li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"><pre>{{ line|escape }}</pre></li>{% endfor %}</ol>
  492. {% endif %}
  493. </div>
  494. {% endif %}
  495. {% if frame.vars %}
  496. <div class="commands">
  497. {% if is_email %}
  498. <h2>Local Vars</h2>
  499. {% else %}
  500. <a href="#" onclick="return varToggle(this, '{{ frame.id }}')"><span>&#x25b6;</span> Local vars</a>
  501. {% endif %}
  502. </div>
  503. <table class="vars" id="v{{ frame.id }}">
  504. <thead>
  505. <tr>
  506. <th>Variable</th>
  507. <th>Value</th>
  508. </tr>
  509. </thead>
  510. <tbody>
  511. {% for var in frame.vars|dictsort:"0" %}
  512. <tr>
  513. <td>{{ var.0|force_escape }}</td>
  514. <td class="code"><pre>{{ var.1|pprint|force_escape }}</pre></td>
  515. </tr>
  516. {% endfor %}
  517. </tbody>
  518. </table>
  519. {% endif %}
  520. </li>
  521. {% endfor %}
  522. </ul>
  523. </div>
  524. {% endautoescape %}
  525. <form action="http://dpaste.com/" name="pasteform" id="pasteform" method="post">
  526. {% if not is_email %}
  527. <div id="pastebinTraceback" class="pastebin">
  528. <input type="hidden" name="language" value="PythonConsole">
  529. <input type="hidden" name="title" value="{{ exception_type|escape }}{% if request %} at {{ request.path_info|escape }}{% endif %}">
  530. <input type="hidden" name="source" value="Django Dpaste Agent">
  531. <input type="hidden" name="poster" value="Django">
  532. <textarea name="content" id="traceback_area" cols="140" rows="25">
  533. Environment:
  534. {% if request %}
  535. Request Method: {{ request.META.REQUEST_METHOD }}
  536. Request URL: {{ request.build_absolute_uri|escape }}
  537. {% endif %}
  538. Django Version: {{ django_version_info }}
  539. Python Version: {{ sys_version_info }}
  540. Installed Applications:
  541. {{ settings.INSTALLED_APPS|pprint }}
  542. Installed Middleware:
  543. {{ settings.MIDDLEWARE_CLASSES|pprint }}
  544. {% if template_does_not_exist %}Template Loader Error:
  545. {% if loader_debug_info %}Django tried loading these templates, in this order:
  546. {% for loader in loader_debug_info %}Using loader {{ loader.loader }}:
  547. {% for t in loader.templates %}{{ t.name }} (File {% if t.exists %}exists{% else %}does not exist{% endif %})
  548. {% endfor %}{% endfor %}
  549. {% else %}Django couldn't find any templates because your TEMPLATE_LOADERS setting is empty!
  550. {% endif %}
  551. {% endif %}{% if template_info %}
  552. Template error:
  553. In template {{ template_info.name }}, error at line {{ template_info.line }}
  554. {{ template_info.message }}{% for source_line in template_info.source_lines %}{% ifequal source_line.0 template_info.line %}
  555. {{ source_line.0 }} : {{ template_info.before }} {{ template_info.during }} {{ template_info.after }}
  556. {% else %}
  557. {{ source_line.0 }} : {{ source_line.1 }}
  558. {% endifequal %}{% endfor %}{% endif %}
  559. Traceback:
  560. {% for frame in frames %}File "{{ frame.filename|escape }}" in {{ frame.function|escape }}
  561. {% if frame.context_line %} {{ frame.lineno }}. {{ frame.context_line|escape }}{% endif %}
  562. {% endfor %}
  563. Exception Type: {{ exception_type|escape }}{% if request %} at {{ request.path_info|escape }}{% endif %}
  564. Exception Value: {{ exception_value|force_escape }}
  565. </textarea>
  566. <br><br>
  567. <input type="submit" value="Share this traceback on a public Web site">
  568. </div>
  569. </form>
  570. </div>
  571. {% endif %}
  572. <div id="requestinfo">
  573. <h2>Request information</h2>
  574. {% if request %}
  575. <h3 id="get-info">GET</h3>
  576. {% if request.GET %}
  577. <table class="req">
  578. <thead>
  579. <tr>
  580. <th>Variable</th>
  581. <th>Value</th>
  582. </tr>
  583. </thead>
  584. <tbody>
  585. {% for var in request.GET.items %}
  586. <tr>
  587. <td>{{ var.0 }}</td>
  588. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  589. </tr>
  590. {% endfor %}
  591. </tbody>
  592. </table>
  593. {% else %}
  594. <p>No GET data</p>
  595. {% endif %}
  596. <h3 id="post-info">POST</h3>
  597. {% if request.POST %}
  598. <table class="req">
  599. <thead>
  600. <tr>
  601. <th>Variable</th>
  602. <th>Value</th>
  603. </tr>
  604. </thead>
  605. <tbody>
  606. {% for var in request.POST.items %}
  607. <tr>
  608. <td>{{ var.0 }}</td>
  609. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  610. </tr>
  611. {% endfor %}
  612. </tbody>
  613. </table>
  614. {% else %}
  615. <p>No POST data</p>
  616. {% endif %}
  617. <h3 id="files-info">FILES</h3>
  618. {% if request.FILES %}
  619. <table class="req">
  620. <thead>
  621. <tr>
  622. <th>Variable</th>
  623. <th>Value</th>
  624. </tr>
  625. </thead>
  626. <tbody>
  627. {% for var in request.FILES.items %}
  628. <tr>
  629. <td>{{ var.0 }}</td>
  630. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  631. </tr>
  632. {% endfor %}
  633. </tbody>
  634. </table>
  635. {% else %}
  636. <p>No FILES data</p>
  637. {% endif %}
  638. <h3 id="cookie-info">COOKIES</h3>
  639. {% if request.COOKIES %}
  640. <table class="req">
  641. <thead>
  642. <tr>
  643. <th>Variable</th>
  644. <th>Value</th>
  645. </tr>
  646. </thead>
  647. <tbody>
  648. {% for var in request.COOKIES.items %}
  649. <tr>
  650. <td>{{ var.0 }}</td>
  651. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  652. </tr>
  653. {% endfor %}
  654. </tbody>
  655. </table>
  656. {% else %}
  657. <p>No cookie data</p>
  658. {% endif %}
  659. <h3 id="meta-info">META</h3>
  660. <table class="req">
  661. <thead>
  662. <tr>
  663. <th>Variable</th>
  664. <th>Value</th>
  665. </tr>
  666. </thead>
  667. <tbody>
  668. {% for var in request.META.items|dictsort:"0" %}
  669. <tr>
  670. <td>{{ var.0 }}</td>
  671. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  672. </tr>
  673. {% endfor %}
  674. </tbody>
  675. </table>
  676. {% endif %}
  677. <h3 id="settings-info">Settings</h3>
  678. <h4>Using settings module <code>{{ settings.SETTINGS_MODULE }}</code></h4>
  679. <table class="req">
  680. <thead>
  681. <tr>
  682. <th>Setting</th>
  683. <th>Value</th>
  684. </tr>
  685. </thead>
  686. <tbody>
  687. {% for var in settings.items|dictsort:"0" %}
  688. <tr>
  689. <td>{{ var.0 }}</td>
  690. <td class="code"><pre>{{ var.1|pprint }}</pre></td>
  691. </tr>
  692. {% endfor %}
  693. </tbody>
  694. </table>
  695. </div>
  696. <div id="explanation">
  697. <p>
  698. You're seeing this error because you have <code>DEBUG = True</code> in your
  699. Django settings file. Change that to <code>False</code>, and Django will
  700. display a standard 500 page.
  701. </p>
  702. </div>
  703. </body>
  704. </html>
  705. """
  706. TECHNICAL_404_TEMPLATE = """
  707. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  708. <html lang="en">
  709. <head>
  710. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  711. <title>Page not found at {{ request.path_info|escape }}</title>
  712. <meta name="robots" content="NONE,NOARCHIVE">
  713. <style type="text/css">
  714. html * { padding:0; margin:0; }
  715. body * { padding:10px 20px; }
  716. body * * { padding:0; }
  717. body { font:small sans-serif; background:#eee; }
  718. body>div { border-bottom:1px solid #ddd; }
  719. h1 { font-weight:normal; margin-bottom:.4em; }
  720. h1 span { font-size:60%; color:#666; font-weight:normal; }
  721. table { border:none; border-collapse: collapse; width:100%; }
  722. td, th { vertical-align:top; padding:2px 3px; }
  723. th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  724. #info { background:#f6f6f6; }
  725. #info ol { margin: 0.5em 4em; }
  726. #info ol li { font-family: monospace; }
  727. #summary { background: #ffc; }
  728. #explanation { background:#eee; border-bottom: 0px none; }
  729. </style>
  730. </head>
  731. <body>
  732. <div id="summary">
  733. <h1>Page not found <span>(404)</span></h1>
  734. <table class="meta">
  735. <tr>
  736. <th>Request Method:</th>
  737. <td>{{ request.META.REQUEST_METHOD }}</td>
  738. </tr>
  739. <tr>
  740. <th>Request URL:</th>
  741. <td>{{ request.build_absolute_uri|escape }}</td>
  742. </tr>
  743. </table>
  744. </div>
  745. <div id="info">
  746. {% if urlpatterns %}
  747. <p>
  748. Using the URLconf defined in <code>{{ urlconf }}</code>,
  749. Django tried these URL patterns, in this order:
  750. </p>
  751. <ol>
  752. {% for pattern in urlpatterns %}
  753. <li>
  754. {% for pat in pattern %}
  755. {{ pat.regex.pattern }}
  756. {% if forloop.last and pat.name %}[name='{{ pat.name }}']{% endif %}
  757. {% endfor %}
  758. </li>
  759. {% endfor %}
  760. </ol>
  761. <p>The current URL, <code>{{ request_path|escape }}</code>, didn't match any of these.</p>
  762. {% else %}
  763. <p>{{ reason }}</p>
  764. {% endif %}
  765. </div>
  766. <div id="explanation">
  767. <p>
  768. You're seeing this error because you have <code>DEBUG = True</code> in
  769. your Django settings file. Change that to <code>False</code>, and Django
  770. will display a standard 404 page.
  771. </p>
  772. </div>
  773. </body>
  774. </html>
  775. """
  776. EMPTY_URLCONF_TEMPLATE = """
  777. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  778. <html lang="en"><head>
  779. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  780. <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
  781. <style type="text/css">
  782. html * { padding:0; margin:0; }
  783. body * { padding:10px 20px; }
  784. body * * { padding:0; }
  785. body { font:small sans-serif; }
  786. body>div { border-bottom:1px solid #ddd; }
  787. h1 { font-weight:normal; }
  788. h2 { margin-bottom:.8em; }
  789. h2 span { font-size:80%; color:#666; font-weight:normal; }
  790. h3 { margin:1em 0 .5em 0; }
  791. h4 { margin:0 0 .5em 0; font-weight: normal; }
  792. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  793. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  794. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  795. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  796. ul { margin-left: 2em; margin-top: 1em; }
  797. #summary { background: #e0ebff; }
  798. #summary h2 { font-weight: normal; color: #666; }
  799. #explanation { background:#eee; }
  800. #instructions { background:#f6f6f6; }
  801. #summary table { border:none; background:transparent; }
  802. </style>
  803. </head>
  804. <body>
  805. <div id="summary">
  806. <h1>It worked!</h1>
  807. <h2>Congratulations on your first Django-powered page.</h2>
  808. </div>
  809. <div id="instructions">
  810. <p>Of course, you haven't actually done any work yet. Here's what to do next:</p>
  811. <ul>
  812. <li>If you plan to use a database, edit the <code>DATABASES</code> setting in <code>{{ project_name }}/settings.py</code>.</li>
  813. <li>Start your first app by running <code>python {{ project_name }}/manage.py startapp [appname]</code>.</li>
  814. </ul>
  815. </div>
  816. <div id="explanation">
  817. <p>
  818. You're seeing this message because you have <code>DEBUG = True</code> in your
  819. Django settings file and you haven't configured any URLs. Get to work!
  820. </p>
  821. </div>
  822. </body></html>
  823. """